diff --git a/src/components/search_bar/query/__snapshots__/ast_to_es_query_dsl.test.ts.snap b/src/components/search_bar/query/__snapshots__/ast_to_es_query_dsl.test.ts.snap index 6299836a17e..127c7d8bf6a 100644 --- a/src/components/search_bar/query/__snapshots__/ast_to_es_query_dsl.test.ts.snap +++ b/src/components/search_bar/query/__snapshots__/ast_to_es_query_dsl.test.ts.snap @@ -11,21 +11,37 @@ Object { "bool": Object { "must": Array [ Object { - "match": Object { - "group": Object { - "operator": "and", - "query": "kibana logstash", - }, + "bool": Object { + "must": Array [ + Object { + "match": Object { + "group": "kibana", + }, + }, + Object { + "match": Object { + "group": "logstash", + }, + }, + ], }, }, ], "must_not": Array [ Object { - "match": Object { - "group": Object { - "operator": "and", - "query": "es beats", - }, + "bool": Object { + "must": Array [ + Object { + "match": Object { + "group": "es", + }, + }, + Object { + "match": Object { + "group": "beats", + }, + }, + ], }, }, ], @@ -44,10 +60,7 @@ Object { }, Object { "match": Object { - "group": Object { - "operator": "and", - "query": "kibana", - }, + "group": "kibana", }, }, Object { @@ -70,11 +83,19 @@ Object { }, }, Object { - "match": Object { - "group": Object { - "operator": "and", - "query": "eng es", - }, + "bool": Object { + "must": Array [ + Object { + "match": Object { + "group": "eng", + }, + }, + Object { + "match": Object { + "group": "es", + }, + }, + ], }, }, Object { @@ -96,10 +117,7 @@ Object { }, Object { "match": Object { - "group": Object { - "operator": "and", - "query": "kibana", - }, + "group": "kibana", }, }, ], @@ -142,10 +160,7 @@ Object { "should": Array [ Object { "match": Object { - "group": Object { - "operator": "or", - "query": "eng", - }, + "group": "eng", }, }, Object { @@ -178,21 +193,26 @@ Object { }, }, Object { - "match": Object { - "group": Object { - "operator": "or", - "query": "eng es", - }, + "bool": Object { + "should": Array [ + Object { + "match": Object { + "group": "eng", + }, + }, + Object { + "match": Object { + "group": "es", + }, + }, + ], }, }, ], "must_not": Array [ Object { "match": Object { - "group": Object { - "operator": "and", - "query": "kibana", - }, + "group": "kibana", }, }, ], @@ -212,10 +232,7 @@ Object { "must": Array [ Object { "match": Object { - "name": Object { - "operator": "and", - "query": "john", - }, + "name": "john", }, }, ], @@ -226,10 +243,7 @@ Object { "must": Array [ Object { "match": Object { - "name": Object { - "operator": "and", - "query": "fred", - }, + "name": "fred", }, }, ], @@ -255,10 +269,7 @@ Object { "must": Array [ Object { "match": Object { - "name": Object { - "operator": "and", - "query": "john", - }, + "name": "john", }, }, ], @@ -414,10 +425,7 @@ Object { "must": Array [ Object { "match": Object { - "name": Object { - "operator": "and", - "query": "john", - }, + "name": "john", }, }, ], diff --git a/src/components/search_bar/query/ast_to_es_query_dsl.ts b/src/components/search_bar/query/ast_to_es_query_dsl.ts index b863d25d685..9b2c89c36f4 100644 --- a/src/components/search_bar/query/ast_to_es_query_dsl.ts +++ b/src/components/search_bar/query/ast_to_es_query_dsl.ts @@ -6,19 +6,19 @@ * Side Public License, v 1. */ -import { printIso8601 } from './date_format'; -import { isDateValue, dateValue, DateValue } from './date_value'; +import { isArray, isDateLike, isString } from '../../../services/predicate'; +import { keysOf } from '../../common'; import { - _AST, AST, FieldClause, IsClause, OperatorType, TermClause, Value, + _AST, } from './ast'; -import { isArray, isDateLike, isString } from '../../../services/predicate'; -import { keysOf } from '../../common'; +import { printIso8601 } from './date_format'; +import { dateValue, DateValue, isDateValue } from './date_value'; export interface QueryContainer { bool?: BoolQuery; @@ -140,15 +140,16 @@ export const _fieldValuesToQuery = ( } }); - if (terms.length > 0) { + if (terms.length > 1) { queries.push({ - match: { - [field]: { - query: terms.join(' '), - operator: andOr, - }, + bool: { + [andOr === 'and' ? 'must' : 'should']: [ + ...terms.map((value) => ({ match: { [field]: value } })), + ], }, }); + } else if (terms.length === 1) { + queries.push({ match: { [field]: terms[0] } }); } if (phrases.length > 0) { diff --git a/upcoming_changelogs/6220.md b/upcoming_changelogs/6220.md new file mode 100644 index 00000000000..1ca89026391 --- /dev/null +++ b/upcoming_changelogs/6220.md @@ -0,0 +1,3 @@ +**Bug fixes** + +- Fixed `Query.toESQuery()` to generate bool queries instead of relying on match query logic, to work with non-text fields