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 8638ac8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 1 deletion.
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 words with quotes -
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 @@ -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\\"",
},
},
],
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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\\""`;
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
9 changes: 8 additions & 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,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
3 changes: 3 additions & 0 deletions src/components/search_bar/query/ast_to_es_query_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
};

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 8638ac8

Please sign in to comment.