Skip to content

Commit

Permalink
Fix Enterprise Search document search bug in indices without position…
Browse files Browse the repository at this point in the history
… data indexed (#148397)

## Summary

Fixes a bug where some searches including spaces would error in certain
Enterprise Search indices that don't support multi-match queries in
indexed metadata.


### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
kderusso and kibanamachine authored Jan 4, 2023
1 parent 9fe3d51 commit 07a19f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
9 changes: 6 additions & 3 deletions x-pack/plugins/enterprise_search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -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,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const fetchSearchResults = async (
from,
index: indexName,
size,
...(!!query ? { q: JSON.stringify(query) } : {}),
...(!!query ? { q: query.replace(/"/g, '\\"') } : {}),
});
return results;
};

0 comments on commit 07a19f1

Please sign in to comment.