Skip to content

Commit

Permalink
handling references for kibana_context and get_index_pattern expressi…
Browse files Browse the repository at this point in the history
…on functions (#95224) (#114447)

# Conflicts:
#	src/plugins/data/server/search/search_service.ts

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ppisljar and kibanamachine authored Oct 13, 2021
1 parent 03eb079 commit 6c34e15
Show file tree
Hide file tree
Showing 95 changed files with 492 additions and 629 deletions.
22 changes: 14 additions & 8 deletions packages/kbn-es-query/src/es_query/build_es_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ describe('build query', () => {
{ query: 'bar:baz', language: 'lucene' },
] as Query[];
const filters = {
match: {
a: 'b',
query: {
match: {
a: 'b',
},
},
meta: {
alias: '',
Expand Down Expand Up @@ -80,8 +82,10 @@ describe('build query', () => {
it('should accept queries and filters as either single objects or arrays', () => {
const queries = { query: 'extension:jpg', language: 'lucene' } as Query;
const filters = {
match: {
a: 'b',
query: {
match: {
a: 'b',
},
},
meta: {
alias: '',
Expand Down Expand Up @@ -118,12 +122,14 @@ describe('build query', () => {
it('should remove match_all clauses', () => {
const filters = [
{
match_all: {},
query: { match_all: {} },
meta: { type: 'match_all' },
} as MatchAllFilter,
{
match: {
a: 'b',
query: {
match: {
a: 'b',
},
},
meta: {
alias: '',
Expand Down Expand Up @@ -163,7 +169,7 @@ describe('build query', () => {
{ query: '@timestamp:"2019-03-23T13:18:00"', language: 'kuery' },
{ query: '@timestamp:"2019-03-23T13:18:00"', language: 'lucene' },
] as Query[];
const filters = [{ match_all: {}, meta: { type: 'match_all' } } as MatchAllFilter];
const filters = [{ query: { match_all: {} }, meta: { type: 'match_all' } } as MatchAllFilter];
const config = {
allowLeadingWildcards: true,
queryStringOptions: {},
Expand Down
14 changes: 7 additions & 7 deletions packages/kbn-es-query/src/es_query/from_filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ describe('build query', () => {
test('should transform an array of kibana filters into ES queries combined in the bool clauses', () => {
const filters = [
{
match_all: {},
query: { match_all: {} },
meta: { type: 'match_all' },
} as MatchAllFilter,
{
exists: { field: 'foo' },
query: { exists: { field: 'foo' } },
meta: { type: 'exists' },
} as ExistsFilter,
] as Filter[];
Expand All @@ -50,7 +50,7 @@ describe('build query', () => {
test('should remove disabled filters', () => {
const filters = [
{
match_all: {},
query: { match_all: {} },
meta: { type: 'match_all', negate: true, disabled: true },
} as MatchAllFilter,
] as Filter[];
Expand All @@ -70,7 +70,7 @@ describe('build query', () => {
test('should place negated filters in the must_not clause', () => {
const filters = [
{
match_all: {},
query: { match_all: {} },
meta: { type: 'match_all', negate: true },
} as MatchAllFilter,
] as Filter[];
Expand Down Expand Up @@ -104,10 +104,10 @@ describe('build query', () => {
test('should migrate deprecated match syntax', () => {
const filters = [
{
query: { match: { extension: { query: 'foo', type: 'phrase' } } },
match: { extension: { query: 'foo', type: 'phrase' } },
meta: { type: 'phrase' },
},
] as Filter[];
] as unknown as Filter[];

const expectedESQueries = [
{
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('build query', () => {
test('should wrap filters targeting nested fields in a nested query', () => {
const filters = [
{
exists: { field: 'nestedField.child' },
query: { exists: { field: 'nestedField.child' } },
meta: { type: 'exists', alias: '', disabled: false, negate: false },
},
];
Expand Down
7 changes: 1 addition & 6 deletions packages/kbn-es-query/src/es_query/from_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ const filterNegate = (reverse: boolean) => (filter: Filter) => {
* @return {Object} the query version of that filter
*/
const translateToQuery = (filter: Partial<Filter>): estypes.QueryDslQueryContainer => {
if (filter.query) {
return filter.query as estypes.QueryDslQueryContainer;
}

// TODO: investigate what's going on here! What does this mean for filters that don't have a query!
return filter as estypes.QueryDslQueryContainer;
return filter.query || filter;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ describe('handleNestedFilter', function () {
// for example, we don't support query_string queries
const filter = buildQueryFilter(
{
query: {
query_string: {
query: 'response:200',
},
query_string: {
query: 'response:200',
},
},
'logstash-*',
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-es-query/src/es_query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

export { migrateFilter } from './migrate_filter';
export { buildEsQuery, EsQueryConfig } from './build_es_query';
export { buildQueryFromFilters } from './from_filters';
export { luceneStringToDsl } from './lucene_string_to_dsl';
Expand Down
16 changes: 14 additions & 2 deletions packages/kbn-es-query/src/es_query/migrate_filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import { PhraseFilter, MatchAllFilter } from '../filters';

describe('migrateFilter', function () {
const oldMatchPhraseFilter = {
match: {
fieldFoo: {
query: 'foobar',
type: 'phrase',
},
},
meta: {},
} as unknown as DeprecatedMatchPhraseFilter;

const oldMatchPhraseFilter2 = {
query: {
match: {
fieldFoo: {
Expand All @@ -36,8 +46,10 @@ describe('migrateFilter', function () {

it('should migrate match filters of type phrase', function () {
const migratedFilter = migrateFilter(oldMatchPhraseFilter, undefined);

expect(migratedFilter).toEqual(newMatchPhraseFilter);

const migratedFilter2 = migrateFilter(oldMatchPhraseFilter2, undefined);
expect(migratedFilter2).toEqual(newMatchPhraseFilter);
});

it('should not modify the original filter', function () {
Expand All @@ -50,7 +62,7 @@ describe('migrateFilter', function () {

it('should return the original filter if no migration is necessary', function () {
const originalFilter = {
match_all: {},
query: { match_all: {} },
} as MatchAllFilter;
const migratedFilter = migrateFilter(originalFilter, undefined);

Expand Down
67 changes: 56 additions & 11 deletions packages/kbn-es-query/src/es_query/migrate_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ import { IndexPatternBase } from './types';

/** @internal */
export interface DeprecatedMatchPhraseFilter extends Filter {
query: {
match: {
[field: string]: {
query: any;
type: 'phrase';
};
match: {
[field: string]: {
query: any;
type: 'phrase';
};
};
}

function isDeprecatedMatchPhraseFilter(filter: Filter): filter is DeprecatedMatchPhraseFilter {
const fieldName = Object.keys(filter.query?.match ?? {})[0];
return Boolean(fieldName && get(filter, ['query', 'match', fieldName, 'type']) === 'phrase');
// @ts-ignore
const fieldName = Object.keys((filter.match || filter.query?.match) ?? {})[0];
return Boolean(
fieldName &&
(get(filter, ['query', 'match', fieldName, 'type']) === 'phrase' ||
get(filter, ['match', fieldName, 'type']) === 'phrase')
);
}

/** @internal */
export function migrateFilter(filter: Filter, indexPattern?: IndexPatternBase) {
if (isDeprecatedMatchPhraseFilter(filter)) {
const fieldName = Object.keys(filter.query.match)[0];
const params: Record<string, any> = get(filter, ['query', 'match', fieldName]);
// @ts-ignore
const match = filter.match || filter.query.match;
const fieldName = Object.keys(match)[0];
const params: Record<string, any> = get(match, [fieldName]);
let query = params.query;
if (indexPattern) {
const field = indexPattern.fields.find((f) => f.name === fieldName);
Expand All @@ -42,7 +47,8 @@ export function migrateFilter(filter: Filter, indexPattern?: IndexPatternBase) {
}
}
return {
...filter,
meta: filter.meta,
$state: filter.$state,
query: {
match_phrase: {
[fieldName]: omit(
Expand All @@ -57,5 +63,44 @@ export function migrateFilter(filter: Filter, indexPattern?: IndexPatternBase) {
};
}

if (!filter.query) {
filter.query = {};
}

// @ts-ignore
if (filter.exists) {
// @ts-ignore
filter.query.exists = filter.exists;
// @ts-ignore
delete filter.exists;
}

// @ts-ignore
if (filter.range) {
// @ts-ignore
filter.query.range = filter.range;
// @ts-ignore
delete filter.range;
}

// @ts-ignore
if (filter.match_all) {
// @ts-ignore
filter.query.match_all = filter.match_all;
// @ts-ignore
delete filter.match_all;
}

// move all other keys under query
Object.keys(filter).forEach((key) => {
if (key === 'meta' || key === 'query' || key === '$state') {
return;
}
// @ts-ignore
filter.query[key] = filter[key];
// @ts-ignore
delete filter[key];
});

return filter;
}
17 changes: 11 additions & 6 deletions packages/kbn-es-query/src/filters/build_filters/exists_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import type { Filter, FilterMeta } from './types';
/** @public */
export type ExistsFilter = Filter & {
meta: FilterMeta;
exists?: {
field: string;
query: {
exists?: {
field: string;
};
};
};

Expand All @@ -24,13 +26,14 @@ export type ExistsFilter = Filter & {
*
* @public
*/
export const isExistsFilter = (filter: Filter): filter is ExistsFilter => has(filter, 'exists');
export const isExistsFilter = (filter: Filter): filter is ExistsFilter =>
has(filter, 'query.exists');

/**
* @internal
*/
export const getExistsFilterField = (filter: ExistsFilter) => {
return filter.exists && filter.exists.field;
return filter.query.exists && filter.query.exists.field;
};

/**
Expand All @@ -46,8 +49,10 @@ export const buildExistsFilter = (field: IndexPatternFieldBase, indexPattern: In
meta: {
index: indexPattern.id,
},
exists: {
field: field.name,
query: {
exists: {
field: field.name,
},
},
} as ExistsFilter;
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ describe('getFilterField', function () {
it('should return undefined for filters that do not target a specific field', () => {
const filter = buildQueryFilter(
{
query: {
query_string: {
query: 'response:200 and extension:jpg',
},
query_string: {
query: 'response:200 and extension:jpg',
},
},
indexPattern.id!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { getExistsFilterField, isExistsFilter } from './exists_filter';
import { getMissingFilterField, isMissingFilter } from './missing_filter';
import { getPhrasesFilterField, isPhrasesFilter } from './phrases_filter';
import { getPhraseFilterField, isPhraseFilter } from './phrase_filter';
import { getRangeFilterField, isRangeFilter } from './range_filter';
Expand All @@ -27,9 +26,6 @@ export const getFilterField = (filter: Filter) => {
if (isRangeFilter(filter)) {
return getRangeFilterField(filter);
}
if (isMissingFilter(filter)) {
return getMissingFilterField(filter);
}

return;
};
1 change: 0 additions & 1 deletion packages/kbn-es-query/src/filters/build_filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export * from './exists_filter';
export * from './get_filter_field';
export * from './get_filter_params';
export * from './match_all_filter';
export * from './missing_filter';
export * from './phrase_filter';
export * from './phrases_filter';
export * from './query_string_filter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export interface MatchAllFilterMeta extends FilterMeta {

export type MatchAllFilter = Filter & {
meta: MatchAllFilterMeta;
match_all: estypes.QueryDslMatchAllQuery;
query: {
match_all: estypes.QueryDslMatchAllQuery;
};
};

/**
Expand All @@ -27,4 +29,4 @@ export type MatchAllFilter = Filter & {
* @public
*/
export const isMatchAllFilter = (filter: Filter): filter is MatchAllFilter =>
has(filter, 'match_all');
has(filter, 'query.match_all');
Loading

0 comments on commit 6c34e15

Please sign in to comment.