Skip to content

Commit

Permalink
Fix track total hits not sent correctly from SearchSource (#91909)
Browse files Browse the repository at this point in the history
* Move track total hits to root level

* code review

* doc
  • Loading branch information
lizozom authored Feb 22, 2021
1 parent 2df74a1 commit b1d76ce
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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> | |

Original file line number Diff line number Diff line change
@@ -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;
```
3 changes: 2 additions & 1 deletion examples/search_examples/public/search/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/search_source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface SearchSourceFields {
sort?: EsQuerySortValue | EsQuerySortValue[];
highlight?: any;
highlightAll?: boolean;
trackTotalHits?: boolean | number;
/**
* {@link AggConfigs}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,8 @@ export interface SearchSourceFields {
// (undocumented)
timeout?: string;
// (undocumented)
trackTotalHits?: boolean | number;
// (undocumented)
type?: string;
// (undocumented)
version?: boolean;
Expand Down

0 comments on commit b1d76ce

Please sign in to comment.