Skip to content

Commit

Permalink
match_all query disappears when typed into Lucene query bar (#62194)
Browse files Browse the repository at this point in the history
* match_all query disappears when typed into Lucene query bar

Closes: #52115

* add migrations for searh savedobject type

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
alexwizp and elasticmachine authored Apr 6, 2020
1 parent 113181b commit fa661c0
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 12 deletions.
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,
'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
32 changes: 32 additions & 0 deletions src/plugins/data/server/saved_objects/search_migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ import { searchSavedObjectTypeMigrations } from './search_migrations';
const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext;

describe('migration search', () => {
describe('6.7.2', () => {
const migrationFn = searchSavedObjectTypeMigrations['6.7.2'];

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

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

describe('7.0.0', () => {
const migrationFn = searchSavedObjectTypeMigrations['7.0.0'];

Expand Down
36 changes: 36 additions & 0 deletions src/plugins/data/server/saved_objects/search_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@

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

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;
};

const migrateIndexPattern: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
Expand Down Expand Up @@ -87,6 +122,7 @@ const migrateSearchSortToNestedArray: SavedObjectMigrationFn = doc => {
};

export const searchSavedObjectTypeMigrations = {
'6.7.2': flow<SavedObjectMigrationFn>(migrateMatchAllQuery),
'7.0.0': flow<SavedObjectMigrationFn>(setNewReferences),
'7.4.0': flow<SavedObjectMigrationFn>(migrateSearchSortToNestedArray),
};
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

0 comments on commit fa661c0

Please sign in to comment.