Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match_all query disappears when typed into Lucene query bar #62194

Merged
merged 5 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/legacy/core_plugins/kibana/migrations/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/

import { get } from 'lodash';
import { migrations730 as dashboardMigrations730 } from '../public/dashboard/migrations';
import {
migrateMatchAllQuery,
migrations730 as dashboardMigrations730,
} from '../public/dashboard/migrations';

function migrateIndexPattern(doc) {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
Expand Down Expand Up @@ -60,6 +63,7 @@ function migrateIndexPattern(doc) {

export const migrations = {
dashboard: {
'6.7.2': migrateMatchAllQuery,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that version '6.7.2' is a correct version, anyway this PR will be delivered only for versions >7.8 so it's not so important

'7.0.0': doc => {
// Set new "references" attribute
doc.references = doc.references || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { migrations730 } from './migrations_730';
export { migrateMatchAllQuery } from './migrate_match_all_query';
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { migrateMatchAllQuery } from './migrate_match_all_query';
import { SavedObjectMigrationContext, SavedObjectMigrationFn } from 'kibana/server';

const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext;

describe('migrate match_all query', () => {
test('should migrate obsolete match_all query', () => {
const migratedDoc = migrateMatchAllQuery(
{
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
} as Parameters<SavedObjectMigrationFn>[0],
savedObjectMigrationContext
);

const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SavedObjectMigrationFn } from 'kibana/server';
import { get } from 'lodash';
import { DEFAULT_QUERY_LANGUAGE } from '../../../../../../plugins/data/common';

export const migrateMatchAllQuery: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get<string>(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

if (searchSourceJSON) {
let searchSource: any;

try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}

if (searchSource.query?.match_all) {
return {
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
...searchSource,
query: {
query: '',
language: DEFAULT_QUERY_LANGUAGE,
},
}),
},
},
};
}
}

return doc;
};
14 changes: 7 additions & 7 deletions src/plugins/data/public/query/lib/to_user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@

import { toUser } from '../';

describe('user input helpers', function() {
describe('model presentation formatter', function() {
it('should present objects as strings', function() {
describe('user input helpers', () => {
describe('model presentation formatter', () => {
test('should present objects as strings', () => {
expect(toUser({ foo: 'bar' })).toBe('{"foo":"bar"}');
});

it('should present query_string queries as strings', function() {
test('should present query_string queries as strings', () => {
expect(toUser({ query_string: { query: 'lucene query string' } })).toBe(
'lucene query string'
);
});

it('should present query_string queries without a query as an empty string', function() {
test('should present query_string queries without a query as an empty string', () => {
expect(toUser({ query_string: {} })).toBe('');
});

it('should present string as strings', function() {
test('should present string as strings', () => {
expect(toUser('foo')).toBe('foo');
});

it('should present numbers as strings', function() {
test('should present numbers as strings', () => {
expect(toUser(400)).toBe('400');
});
});
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/data/public/query/lib/to_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export function toUser(text: { [key: string]: any } | string | number): string {
return '';
}
if (typeof text === 'object') {
if (text.match_all) {
return '';
}
if (text.query_string) {
return toUser(text.query_string.query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ describe('migration visualization', () => {
expect(aggs[3]).not.toHaveProperty('params.customBucket.params.time_zone');
expect(aggs[2]).not.toHaveProperty('params.time_zone');
});

it('should migrate obsolete match_all query', () => {
const migratedDoc = migrate({
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
});
const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { SavedObjectMigrationFn } from 'kibana/server';
import { cloneDeep, get, omit, has, flow } from 'lodash';
import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common';

const migrateIndexPattern: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
Expand Down Expand Up @@ -539,6 +540,40 @@ const migrateTableSplits: SavedObjectMigrationFn = doc => {
}
};

const migrateMatchAllQuery: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get<string>(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

if (searchSourceJSON) {
let searchSource: any;

try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}

if (searchSource.query?.match_all) {
return {
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
...searchSource,
query: {
query: '',
language: DEFAULT_QUERY_LANGUAGE,
},
}),
},
},
};
}
}

return doc;
};

export const visualizationSavedObjectTypeMigrations = {
/**
* We need to have this migration twice, once with a version prior to 7.0.0 once with a version
Expand All @@ -550,7 +585,7 @@ export const visualizationSavedObjectTypeMigrations = {
* in that version. So we apply this twice, once with 6.7.2 and once with 7.0.1 while the backport to 6.7
* only contained the 6.7.2 migration and not the 7.0.1 migration.
*/
'6.7.2': flow<SavedObjectMigrationFn>(removeDateHistogramTimeZones),
'6.7.2': flow<SavedObjectMigrationFn>(migrateMatchAllQuery, removeDateHistogramTimeZones),
'7.0.0': flow<SavedObjectMigrationFn>(
addDocReferences,
migrateIndexPattern,
Expand Down