From c29cc3f37ac74eb9474688a6072b48c1d60e3666 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 5 Jan 2023 15:09:18 -0500 Subject: [PATCH] [8.6] Fix Enterprise Search document search bug in indices without position data indexed (#148397) (#148411) # Backport This will backport the following commits from `main` to `8.6`: - [Fix Enterprise Search document search bug in indices without position data indexed (#148397)](https://github.com/elastic/kibana/pull/148397) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Kathleen DeRusso <63422879+kderusso@users.noreply.github.com> --- x-pack/plugins/enterprise_search/README.md | 9 ++++--- .../server/lib/fetch_search_results.test.ts | 25 +++++++++++++++++-- .../server/lib/fetch_search_results.ts | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/enterprise_search/README.md b/x-pack/plugins/enterprise_search/README.md index 76160d3fcc8db..5694258001dd4 100644 --- a/x-pack/plugins/enterprise_search/README.md +++ b/x-pack/plugins/enterprise_search/README.md @@ -49,11 +49,14 @@ To debug Kea state in-browser, Kea recommends [Redux Devtools](https://v2.keajs. Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing -Jest tests can be run directly from the `x-pack/plugins/enterprise_search` folder. This also works for any subfolders or subcomponents. +Jest tests can be run from the root kibana directory, however, since the tests take so long to run you will likely want to apply the appropriate Jest configuration file to test only your changes. For example: +- `x-pack/plugins/enterprise_search/common/jest.config.js` +- `x-pack/plugins/enterprise_search/public/jest.config.js` +- `x-pack/plugins/enterprise_search/server/jest.config.js` ```bash -yarn test:jest -yarn test:jest --watch +yarn test:jest --config {YOUR_JEST_CONFIG_FILE} +yarn test:jest --config {YOUR_JEST_CONFIG_FILE} --watch ``` Unfortunately coverage collection does not work as automatically, and requires using our handy jest.sh script if you want to run tests on a specific file or folder and only get coverage numbers for that file or folder: diff --git a/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.test.ts b/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.test.ts index 2e3b7a202f153..1f7f1eec9cdff 100644 --- a/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.test.ts +++ b/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.test.ts @@ -87,7 +87,28 @@ describe('fetchSearchResults lib function', () => { expect(mockClient.asCurrentUser.search).toHaveBeenCalledWith({ from: DEFAULT_FROM_VALUE, index: indexName, - q: JSON.stringify(query), + q: query, + size: ENTERPRISE_SEARCH_DOCUMENTS_DEFAULT_DOC_COUNT, + }); + }); + + it('should escape quotes in queries and return results with hits', async () => { + mockClient.asCurrentUser.search.mockImplementation( + () => regularSearchResultsResponse as SearchResponseBody + ); + + await expect( + fetchSearchResults( + mockClient as unknown as IScopedClusterClient, + indexName, + '"yellow banana"' + ) + ).resolves.toEqual(regularSearchResultsResponse); + + expect(mockClient.asCurrentUser.search).toHaveBeenCalledWith({ + from: DEFAULT_FROM_VALUE, + index: indexName, + q: '\\"yellow banana\\"', size: ENTERPRISE_SEARCH_DOCUMENTS_DEFAULT_DOC_COUNT, }); }); @@ -120,7 +141,7 @@ describe('fetchSearchResults lib function', () => { expect(mockClient.asCurrentUser.search).toHaveBeenCalledWith({ from: DEFAULT_FROM_VALUE, index: indexName, - q: JSON.stringify(query), + q: query, size: ENTERPRISE_SEARCH_DOCUMENTS_DEFAULT_DOC_COUNT, }); }); diff --git a/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.ts b/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.ts index b7c3bf81e0ca5..a1b8da77f887d 100644 --- a/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.ts +++ b/x-pack/plugins/enterprise_search/server/lib/fetch_search_results.ts @@ -21,7 +21,7 @@ export const fetchSearchResults = async ( from, index: indexName, size, - ...(!!query ? { q: JSON.stringify(query) } : {}), + ...(!!query ? { q: query.replace(/"/g, '\\"') } : {}), }); return results; };