[data.search] Respect search request track_total_hits value (#215245)

## Summary

Fixes https://github.com/elastic/kibana/issues/212946.

Updates `getSearchParamsFromRequest` to respect `track_total_hits` if it
is included in the `body` portion of the search request.

### Checklist

- [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
This commit is contained in:
Lukas Olson 2025-03-25 09:59:19 -07:00 committed by GitHub
parent 11749b6855
commit fab3060544
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View file

@ -26,7 +26,7 @@ describe('getSearchParams', () => {
expect(searchParams.preference).toBe('aaa');
});
test('extracts track total hits', () => {
test('extracts track total hits from request', () => {
const getConfig = getConfigStub({
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',
[UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE]: 'aaa',
@ -44,6 +44,27 @@ describe('getSearchParams', () => {
expect(searchParams.query).toStrictEqual([{ query: '123', language: 'kql' }]);
});
test('extracts track total hits from body', () => {
const getConfig = getConfigStub({
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',
[UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE]: 'aaa',
});
const searchParams = getSearchParamsFromRequest(
{
body: {
query: { bool: { filter: [] } },
track_total_hits: false,
size: 500,
},
index: 'abc',
},
{ getConfig }
);
expect(searchParams.index).toBe('abc');
expect(searchParams.track_total_hits).toBe(false);
expect(searchParams.query).toMatchInlineSnapshot(`undefined`);
});
test('sets expand_wildcards=all if data view has allowHidden=true', () => {
const getConfig = getConfigStub({
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',

View file

@ -35,10 +35,12 @@ export function getSearchParamsFromRequest(
const searchParams = { preference: getEsPreference(getConfig) };
const dataView = typeof searchRequest.index !== 'string' ? searchRequest.index : undefined;
const index = dataView?.title ?? `${searchRequest.index}`;
const trackTotalHits = searchRequest.track_total_hits ?? searchRequest.body.track_total_hits;
// @ts-expect-error elasticsearch@9.0.0 `query` types don't match (it seems like it's been wrong here for a while)
return {
...searchRequest,
track_total_hits: trackTotalHits,
index,
...(dataView?.getAllowHidden() && { expand_wildcards: 'all' }),
...searchParams,