Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiSearchBar] Fix quoted phrases (without a field) removing quotes #6714

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions upcoming_changelogs/6714.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**Bug fixes**

- Fixed an issue with `EuiSearchBar` where quoted phrases were not quoted when generating an Elasticsearch query.