Fix track total hits not sent correctly from SearchSource (#91909)

* Move track total hits to root level

* code review

* doc
This commit is contained in:
Liza Katz 2021-02-22 23:05:32 +02:00 committed by GitHub
parent 2df74a1088
commit b1d76cedac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 3 deletions

View file

@ -32,6 +32,7 @@ export interface SearchSourceFields
| [source](./kibana-plugin-plugins-data-public.searchsourcefields.source.md) | <code>NameList</code> | |
| [terminate\_after](./kibana-plugin-plugins-data-public.searchsourcefields.terminate_after.md) | <code>number</code> | |
| [timeout](./kibana-plugin-plugins-data-public.searchsourcefields.timeout.md) | <code>string</code> | |
| [trackTotalHits](./kibana-plugin-plugins-data-public.searchsourcefields.tracktotalhits.md) | <code>boolean &#124; number</code> | |
| [type](./kibana-plugin-plugins-data-public.searchsourcefields.type.md) | <code>string</code> | |
| [version](./kibana-plugin-plugins-data-public.searchsourcefields.version.md) | <code>boolean</code> | |

View file

@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [SearchSourceFields](./kibana-plugin-plugins-data-public.searchsourcefields.md) &gt; [trackTotalHits](./kibana-plugin-plugins-data-public.searchsourcefields.tracktotalhits.md)
## SearchSourceFields.trackTotalHits property
<b>Signature:</b>
```typescript
trackTotalHits?: boolean | number;
```

View file

@ -190,7 +190,8 @@ export const SearchExamplesApp = ({
.setField('index', indexPattern)
.setField('filter', filters)
.setField('query', query)
.setField('fields', selectedFields.length ? selectedFields.map((f) => f.name) : ['*']);
.setField('fields', selectedFields.length ? selectedFields.map((f) => f.name) : ['*'])
.setField('trackTotalHits', 100);
if (selectedNumericField) {
searchSource.setField('aggs', () => {

View file

@ -8,7 +8,7 @@
import { UI_SETTINGS } from '../../../constants';
import { GetConfigFn } from '../../../types';
import { getSearchParams } from './get_search_params';
import { getSearchParams, getSearchParamsFromRequest } from './get_search_params';
function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
@ -23,4 +23,26 @@ describe('getSearchParams', () => {
const searchParams = getSearchParams(config);
expect(searchParams.preference).toBe('aaa');
});
test('extracts track total hits', () => {
const getConfig = getConfigStub({
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',
[UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE]: 'aaa',
});
const searchParams = getSearchParamsFromRequest(
{
index: 'abc',
body: {
query: 123,
track_total_hits: true,
},
},
{ getConfig }
);
expect(searchParams.index).toBe('abc');
expect(searchParams.track_total_hits).toBe(true);
expect(searchParams.body).toStrictEqual({
query: 123,
});
});
});

View file

@ -36,10 +36,13 @@ export function getSearchParamsFromRequest(
): ISearchRequestParams {
const { getConfig } = dependencies;
const searchParams = getSearchParams(getConfig);
// eslint-disable-next-line @typescript-eslint/naming-convention
const { track_total_hits, ...body } = searchRequest.body;
return {
index: searchRequest.index.title || searchRequest.index,
body: searchRequest.body,
body,
track_total_hits,
...searchParams,
};
}

View file

@ -429,6 +429,8 @@ export class SearchSource {
return key && data[key] == null && addToRoot(key, val);
case 'searchAfter':
return addToBody('search_after', val);
case 'trackTotalHits':
return addToBody('track_total_hits', val);
case 'source':
return addToBody('_source', val);
case 'sort':

View file

@ -74,6 +74,7 @@ export interface SearchSourceFields {
sort?: EsQuerySortValue | EsQuerySortValue[];
highlight?: any;
highlightAll?: boolean;
trackTotalHits?: boolean | number;
/**
* {@link AggConfigs}
*/

View file

@ -2447,6 +2447,8 @@ export interface SearchSourceFields {
// (undocumented)
timeout?: string;
// (undocumented)
trackTotalHits?: boolean | number;
// (undocumented)
type?: string;
// (undocumented)
version?: boolean;