From 9e26b63bd3c175ef43d129f6ee76ca414ee91237 Mon Sep 17 00:00:00 2001 From: Samuel Nelson Date: Tue, 18 Apr 2023 19:53:12 +1200 Subject: [PATCH] Specify `AND` as `default_operator` for `simple_query_string` --- .../views/search_bar/search_bar_example.js | 3 +- .../ast_to_es_query_dsl.test.ts.snap | 57 +++++++++++++++---- .../query/ast_to_es_query_dsl.test.ts | 9 +++ .../search_bar/query/ast_to_es_query_dsl.ts | 9 ++- upcoming_changelogs/6717.md | 4 ++ 5 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 upcoming_changelogs/6717.md diff --git a/src-docs/src/views/search_bar/search_bar_example.js b/src-docs/src/views/search_bar/search_bar_example.js index a3fc5cb4456..28c8e9a7e8d 100644 --- a/src-docs/src/views/search_bar/search_bar_example.js +++ b/src-docs/src/views/search_bar/search_bar_example.js @@ -41,7 +41,8 @@ export const SearchBarExample = { (free text words) - Example, website -production. In this example the intention is to find all items that have the "website" - terms in them but do not have the word "production" + terms in them but do not have the word "production". + Terms are AND'd together by default.
  • Phrases can be matched by surrounding multiple words with quotes - 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 aa9c884c0fd..3679a788902 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 @@ -6,6 +6,43 @@ Object { } `; +exports[`astToEsQueryDsl ast - '(john OR -mary)' 1`] = ` +Object { + "bool": Object { + "must": Array [ + Object { + "bool": Object { + "should": Array [ + Object { + "bool": Object { + "must": Array [ + Object { + "simple_query_string": Object { + "query": "+john", + }, + }, + ], + }, + }, + Object { + "bool": Object { + "must_not": Array [ + Object { + "simple_query_string": Object { + "query": "+mary", + }, + }, + ], + }, + }, + ], + }, + }, + ], + }, +} +`; + exports[`astToEsQueryDsl ast - '-group:es group:kibana -group:beats group:logstash' 1`] = ` Object { "bool": Object { @@ -67,7 +104,7 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "john", + "query": "+john", }, }, Object { @@ -94,7 +131,7 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "john", + "query": "+john", }, }, Object { @@ -133,7 +170,7 @@ Object { "must_not": Array [ Object { "simple_query_string": Object { - "query": "doe", + "query": "+doe", }, }, Object { @@ -155,14 +192,14 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "john", + "query": "+john", }, }, ], "must_not": Array [ Object { "simple_query_string": Object { - "query": "sales", + "query": "+sales", }, }, ], @@ -176,7 +213,7 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "john", + "query": "+john", }, }, Object { @@ -216,7 +253,7 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "john", + "query": "+john", }, }, Object { @@ -498,7 +535,7 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "Teacher", + "query": "+Teacher", }, }, ], @@ -518,14 +555,14 @@ Object { "must": Array [ Object { "simple_query_string": Object { - "query": "\\"john smith\\"", + "query": "+\\"john smith\\"", }, }, ], "must_not": Array [ Object { "simple_query_string": Object { - "query": "\\"sales team\\"", + "query": "+\\"sales team\\"", }, }, ], diff --git a/src/components/search_bar/query/ast_to_es_query_dsl.test.ts b/src/components/search_bar/query/ast_to_es_query_dsl.test.ts index 2b57a362005..e0fae3c361a 100644 --- a/src/components/search_bar/query/ast_to_es_query_dsl.test.ts +++ b/src/components/search_bar/query/ast_to_es_query_dsl.test.ts @@ -32,6 +32,15 @@ describe('astToEsQueryDsl', () => { expect(query).toMatchSnapshot(); }); + test("ast - '(john OR -mary)'", () => { + const query = astToEsQueryDsl( + AST.create([ + AST.Group.must([AST.Term.must('john'), AST.Term.mustNot('mary')]), + ]) + ); + expect(query).toMatchSnapshot(); + }); + test("ast - '-group:es group:kibana -group:beats group:logstash'", () => { const query = astToEsQueryDsl( AST.create([ 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 12cca9e0610..ea32ea2552c 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 @@ -99,13 +99,16 @@ const processDateOperation = (value: DateValue, operator?: OperatorType) => { }; export const _termValuesToQuery = (values: Value[], options: Options) => { - const body: { query: string; fields?: string[] } = { + const body: { + query: string; + fields?: string[]; + } = { query: values .map((value: Value) => { if (isString(value) && value.match(/\s/)) { - return `"${value}"`; + return `+"${value}"`; } - return value; + return `+${value}`; }) .join(' '), }; diff --git a/upcoming_changelogs/6717.md b/upcoming_changelogs/6717.md new file mode 100644 index 00000000000..08266586283 --- /dev/null +++ b/upcoming_changelogs/6717.md @@ -0,0 +1,4 @@ +**Bug fixes** + +- Fixed inconsistency in AND/OR semantics between DSL and query string generation by the search bar component. +