Skip to content

Commit

Permalink
Quote simple query string terms
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nel committed Apr 16, 2023
1 parent 6a65a47 commit 5fa5c7c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src-docs/src/views/search_bar/search_bar_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
</li>
<li>
Phrases can be matched by surrounding multiple terms with quotes;
for example <EuiCode>&quot;website url&quot;</EuiCode>
</li>
<li>
Field/value search - one can search for terms within specific
fields - Example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Object {
"must": Array [
Object {
"simple_query_string": Object {
"query": "john",
"query": "\\"john\\"",
},
},
Object {
Expand All @@ -94,7 +94,7 @@ Object {
"must": Array [
Object {
"simple_query_string": Object {
"query": "john",
"query": "\\"john\\"",
},
},
Object {
Expand Down Expand Up @@ -133,7 +133,7 @@ Object {
"must_not": Array [
Object {
"simple_query_string": Object {
"query": "doe",
"query": "\\"doe\\"",
},
},
Object {
Expand All @@ -155,14 +155,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\\"",
},
},
],
Expand All @@ -176,7 +176,7 @@ Object {
"must": Array [
Object {
"simple_query_string": Object {
"query": "john",
"query": "\\"john\\"",
},
},
Object {
Expand Down Expand Up @@ -216,7 +216,7 @@ Object {
"must": Array [
Object {
"simple_query_string": Object {
"query": "john",
"query": "\\"john\\"",
},
},
Object {
Expand Down Expand Up @@ -498,7 +498,7 @@ Object {
"must": Array [
Object {
"simple_query_string": Object {
"query": "Teacher",
"query": "\\"Teacher\\"",
},
},
],
Expand All @@ -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\\"",
},
},
],
},
}
`;
7 changes: 7 additions & 0 deletions src/components/search_bar/query/ast_to_es_query_dsl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion src/components/search_bar/query/ast_to_es_query_dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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((v) => `"${v}"`).join(' '),
};
if (body.query === '') {
return;
Expand Down
8 changes: 8 additions & 0 deletions upcoming_changelogs/6714.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**Bug fixes**

- Fixed an issue with search bar where quoted phrases were not quoted when generating an Elasticsearch query.

**Breaking changes**

- Changed search bar Elasticsearch query generation so that quoted phrases now will generate an Elasticsearch query that matches the entire phrase.

0 comments on commit 5fa5c7c

Please sign in to comment.