[8.5] [Dashboard] [Controls] Fix unexpected suggestions for text/keyword multi-fields (#145177) (#145298)

# Backport

This will backport the following commits from `main` to `8.5`:
- [[Dashboard] [Controls] Fix unexpected suggestions for text/keyword
multi-fields (#145177)](https://github.com/elastic/kibana/pull/145177)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Hannah
Mudge","email":"Heenawter@users.noreply.github.com"},"sourceCommit":{"committedDate":"2022-11-15T21:11:45Z","message":"[Dashboard]
[Controls] Fix unexpected suggestions for text/keyword multi-fields
(#145177)\n\nCloses
https://github.com/elastic/kibana/issues/143919\r\n\r\n##
Summary\r\n\r\nThis PR makes it so that the search string is no longer
unnecessarily\r\nescaped for `text /
keyword`\r\n[multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html#text-multi-fields)\r\nwhen
building the options list suggestions.\r\n\r\nFor `keyword` fields,
escaping the search string **is** necessary since\r\nthe `terms`
aggregation treats the `include` property as a regular\r\nexpression.
However, in order to support case-insensitive search for\r\n`text /
keyword` multi-fields, we use the `match_phrase_prefix`
filter\r\ninstead - this query will tokenize the search string as part
of the text\r\nanalysis (as described in
the\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)),\r\nwhich
does not require escaping. Hence, why you would get
unexpected\r\nresults when a `text / keyword` multi-field data contained
escapable\r\ncharacters.\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/8698078/201755604-d2396cdb-50ec-415a-bbed-655c85bde3ad.png)\r\n\r\n**How
to Test**\r\n1. Create an index with that has a field mapped as a `text
/ keyword`\r\nmulti-field with some documents where this field has
punctuation that\r\nwould normally be escaped - for example,
download\r\n\r\n[testPunctuation.csv](10006642/testPunctuation.csv)\r\nand
upload it to Kibana, ensuring you update the auto-mapping to be:\r\n
```json\r\n {\r\n \"id\": {\r\n \"type\": \"long\"\r\n },\r\n
\"sentence\": {\r\n \"type\": \"text\",\r\n \"fields\": {\r\n
\"keyword\": {\r\n \"type\": \"keyword\"\r\n }\r\n }\r\n }\r\n }\r\n
```\r\n2. Create a control on this `text / keyword` multi-field - for
example,\r\non the `sentence.keyword` field if using the provided sample
data\r\n3. Ensure that various searches return reasonable suggestions.
Please\r\nnote that using a `match_phrase_prefix` for an
auto-complete\r\nfunctionality, as we are doing in this instance,
**can** still give some\r\nslightly unexpected results - this is
described in more detail in
the\r\nrelevant\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete).\r\nHowever,
since case-insensitive search is desired, this is a
necessary\r\ntrade-off for supporting this.\r\n\r\n###
Checklist\r\n\r\n- [x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6efec4bb84363e9d31f599d822ace0fda90d47fe","branchLabelMapping":{"^v8.6.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","Feature:Dashboard","release_note:fix","Feature:Input
Control","Team:Presentation","loe:days","impact:low","Project:Controls","backport:prev-minor","v8.6.0"],"number":145177,"url":"https://github.com/elastic/kibana/pull/145177","mergeCommit":{"message":"[Dashboard]
[Controls] Fix unexpected suggestions for text/keyword multi-fields
(#145177)\n\nCloses
https://github.com/elastic/kibana/issues/143919\r\n\r\n##
Summary\r\n\r\nThis PR makes it so that the search string is no longer
unnecessarily\r\nescaped for `text /
keyword`\r\n[multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html#text-multi-fields)\r\nwhen
building the options list suggestions.\r\n\r\nFor `keyword` fields,
escaping the search string **is** necessary since\r\nthe `terms`
aggregation treats the `include` property as a regular\r\nexpression.
However, in order to support case-insensitive search for\r\n`text /
keyword` multi-fields, we use the `match_phrase_prefix`
filter\r\ninstead - this query will tokenize the search string as part
of the text\r\nanalysis (as described in
the\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)),\r\nwhich
does not require escaping. Hence, why you would get
unexpected\r\nresults when a `text / keyword` multi-field data contained
escapable\r\ncharacters.\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/8698078/201755604-d2396cdb-50ec-415a-bbed-655c85bde3ad.png)\r\n\r\n**How
to Test**\r\n1. Create an index with that has a field mapped as a `text
/ keyword`\r\nmulti-field with some documents where this field has
punctuation that\r\nwould normally be escaped - for example,
download\r\n\r\n[testPunctuation.csv](10006642/testPunctuation.csv)\r\nand
upload it to Kibana, ensuring you update the auto-mapping to be:\r\n
```json\r\n {\r\n \"id\": {\r\n \"type\": \"long\"\r\n },\r\n
\"sentence\": {\r\n \"type\": \"text\",\r\n \"fields\": {\r\n
\"keyword\": {\r\n \"type\": \"keyword\"\r\n }\r\n }\r\n }\r\n }\r\n
```\r\n2. Create a control on this `text / keyword` multi-field - for
example,\r\non the `sentence.keyword` field if using the provided sample
data\r\n3. Ensure that various searches return reasonable suggestions.
Please\r\nnote that using a `match_phrase_prefix` for an
auto-complete\r\nfunctionality, as we are doing in this instance,
**can** still give some\r\nslightly unexpected results - this is
described in more detail in
the\r\nrelevant\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete).\r\nHowever,
since case-insensitive search is desired, this is a
necessary\r\ntrade-off for supporting this.\r\n\r\n###
Checklist\r\n\r\n- [x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6efec4bb84363e9d31f599d822ace0fda90d47fe"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.6.0","labelRegex":"^v8.6.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/145177","number":145177,"mergeCommit":{"message":"[Dashboard]
[Controls] Fix unexpected suggestions for text/keyword multi-fields
(#145177)\n\nCloses
https://github.com/elastic/kibana/issues/143919\r\n\r\n##
Summary\r\n\r\nThis PR makes it so that the search string is no longer
unnecessarily\r\nescaped for `text /
keyword`\r\n[multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html#text-multi-fields)\r\nwhen
building the options list suggestions.\r\n\r\nFor `keyword` fields,
escaping the search string **is** necessary since\r\nthe `terms`
aggregation treats the `include` property as a regular\r\nexpression.
However, in order to support case-insensitive search for\r\n`text /
keyword` multi-fields, we use the `match_phrase_prefix`
filter\r\ninstead - this query will tokenize the search string as part
of the text\r\nanalysis (as described in
the\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)),\r\nwhich
does not require escaping. Hence, why you would get
unexpected\r\nresults when a `text / keyword` multi-field data contained
escapable\r\ncharacters.\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/8698078/201755604-d2396cdb-50ec-415a-bbed-655c85bde3ad.png)\r\n\r\n**How
to Test**\r\n1. Create an index with that has a field mapped as a `text
/ keyword`\r\nmulti-field with some documents where this field has
punctuation that\r\nwould normally be escaped - for example,
download\r\n\r\n[testPunctuation.csv](10006642/testPunctuation.csv)\r\nand
upload it to Kibana, ensuring you update the auto-mapping to be:\r\n
```json\r\n {\r\n \"id\": {\r\n \"type\": \"long\"\r\n },\r\n
\"sentence\": {\r\n \"type\": \"text\",\r\n \"fields\": {\r\n
\"keyword\": {\r\n \"type\": \"keyword\"\r\n }\r\n }\r\n }\r\n }\r\n
```\r\n2. Create a control on this `text / keyword` multi-field - for
example,\r\non the `sentence.keyword` field if using the provided sample
data\r\n3. Ensure that various searches return reasonable suggestions.
Please\r\nnote that using a `match_phrase_prefix` for an
auto-complete\r\nfunctionality, as we are doing in this instance,
**can** still give some\r\nslightly unexpected results - this is
described in more detail in
the\r\nrelevant\r\n[documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html#match-phrase-prefix-autocomplete).\r\nHowever,
since case-insensitive search is desired, this is a
necessary\r\ntrade-off for supporting this.\r\n\r\n###
Checklist\r\n\r\n- [x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"6efec4bb84363e9d31f599d822ace0fda90d47fe"}}]}]
BACKPORT-->

Co-authored-by: Hannah Mudge <Heenawter@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-11-15 17:18:02 -05:00 committed by GitHub
parent 65f421f31f
commit f9342f2a65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -103,7 +103,7 @@ const suggestionAggSubtypes: { [key: string]: OptionsListAggregationBuilder } =
return {
filter: {
match_phrase_prefix: {
[textFieldName]: getEscapedQuery(searchString),
[textFieldName]: searchString,
},
},
aggs: {