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 bc20fe5634e..a3fc5cb4456 100644 --- a/src-docs/src/views/search_bar/search_bar_example.js +++ b/src-docs/src/views/search_bar/search_bar_example.js @@ -43,6 +43,10 @@ export const SearchBarExample = { intention is to find all items that have the "website" terms in them but do not have the word "production" +
  • + Phrases can be matched by surrounding multiple words with quotes - + Example, "website url". +
  • Field/value search - one can search for terms within specific fields - Example, 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 f2453736d1a..aa9c884c0fd 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 @@ -511,3 +511,24 @@ Object { }, } `; + +exports[`astToEsQueryDsl ast·-·'"john·smith"·-"sales team"' 1`] = ` +Object { + "bool": Object { + "must": Array [ + Object { + "simple_query_string": Object { + "query": "\\"john smith\\"", + }, + }, + ], + "must_not": Array [ + Object { + "simple_query_string": Object { + "query": "\\"sales team\\"", + }, + }, + ], + }, +} +`; diff --git a/src/components/search_bar/query/__snapshots__/ast_to_es_query_string.test.ts.snap b/src/components/search_bar/query/__snapshots__/ast_to_es_query_string.test.ts.snap index 69990f64ce2..1c81d9d3778 100644 --- a/src/components/search_bar/query/__snapshots__/ast_to_es_query_string.test.ts.snap +++ b/src/components/search_bar/query/__snapshots__/ast_to_es_query_string.test.ts.snap @@ -23,3 +23,5 @@ exports[`astToEsQueryString ast - date:'2004-03' -date<'2004-03-10' 1`] = `"+dat exports[`astToEsQueryString ast - date>'2004-02' -otherDate>='2004-03-10' 1`] = `"+date:>=2004-03 -date:>=2004-03-10"`; exports[`astToEsQueryString ast - date>='2004-03-22' 1`] = `"+date:>=2004-03-22"`; + +exports[`astToEsQueryString ast·-·'"john·smith"·-"sales team"' 1`] = `"+\\"john smith\\" -\\"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 9bab41894b6..2b57a362005 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 @@ -25,6 +25,13 @@ describe('astToEsQueryDsl', () => { expect(query).toMatchSnapshot(); }); + test('ast·-·\'"john·smith"·-"sales team"\'', () => { + const query = astToEsQueryDsl( + AST.create([AST.Term.must('john smith'), AST.Term.mustNot('sales team')]) + ); + 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 722786eb6e1..12cca9e0610 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 @@ -100,7 +100,14 @@ const processDateOperation = (value: DateValue, operator?: OperatorType) => { export const _termValuesToQuery = (values: Value[], options: Options) => { const body: { query: string; fields?: string[] } = { - query: values.join(' '), + query: values + .map((value: Value) => { + if (isString(value) && value.match(/\s/)) { + return `"${value}"`; + } + return value; + }) + .join(' '), }; if (body.query === '') { return; diff --git a/src/components/search_bar/query/ast_to_es_query_string.test.ts b/src/components/search_bar/query/ast_to_es_query_string.test.ts index ed354d335d2..d62f07d6173 100644 --- a/src/components/search_bar/query/ast_to_es_query_string.test.ts +++ b/src/components/search_bar/query/ast_to_es_query_string.test.ts @@ -25,6 +25,13 @@ describe('astToEsQueryString', () => { expect(query).toMatchSnapshot(); }); + test('ast·-·\'"john·smith"·-"sales team"\'', () => { + const query = astToEsQueryString( + AST.create([AST.Term.must('john smith'), AST.Term.mustNot('sales team')]) + ); + expect(query).toMatchSnapshot(); + }); + test("ast - '-group:es group:kibana -group:beats group:logstash'", () => { const query = astToEsQueryString( AST.create([ diff --git a/src/components/search_bar/query/ast_to_es_query_string.ts b/src/components/search_bar/query/ast_to_es_query_string.ts index 3f5fe82bdf8..52209450451 100644 --- a/src/components/search_bar/query/ast_to_es_query_string.ts +++ b/src/components/search_bar/query/ast_to_es_query_string.ts @@ -201,6 +201,9 @@ const emitTermClause = (clause: TermClause, isGroupMember: boolean): string => { } const matchOp = emitMatch(match); + if (isString(value) && value.match(/\s/)) { + return `${matchOp}"${escapeValue(value)}"`; + } return `${matchOp}${escapeValue(value)}`; }; diff --git a/upcoming_changelogs/6714.md b/upcoming_changelogs/6714.md new file mode 100644 index 00000000000..0dc80c5c7c6 --- /dev/null +++ b/upcoming_changelogs/6714.md @@ -0,0 +1,4 @@ +**Bug fixes** + +- Fixed an issue with `EuiSearchBar` where quoted phrases were not quoted when generating an Elasticsearch query. +