Skip to content

Commit

Permalink
[Discover][Context] Set trackTotalHits to false in requests to Elasti…
Browse files Browse the repository at this point in the history
…csearch (#108661) (#109679)

Co-authored-by: Matthias Wilhelm <[email protected]>
  • Loading branch information
kibanamachine and kertal authored Aug 23, 2021
1 parent c31154b commit dfa1e83
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 46 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import { EsQuerySortValue, SortDirection } from '../../../../../../data/public';
import { createIndexPatternsStub, createSearchSourceStub } from './_stubs';
import { fetchAnchorProvider } from './anchor';
import { fetchAnchorProvider, updateSearchSource } from './anchor';
import { EsHitRecord, EsHitRecordList } from './context';
import { indexPatternMock } from '../../../../__mocks__/index_pattern';
import { savedSearchMock } from '../../../../__mocks__/saved_search';

describe('context app', function () {
let fetchAnchor: (
Expand Down Expand Up @@ -114,6 +116,32 @@ describe('context app', function () {
});
});

it('should update search source correctly when useNewFieldsApi set to false', function () {
const searchSource = updateSearchSource(
savedSearchMock.searchSource,
'id',
[],
false,
indexPatternMock
);
const searchRequestBody = searchSource.getSearchRequestBody();
expect(searchRequestBody._source).toBeInstanceOf(Object);
expect(searchRequestBody.track_total_hits).toBe(false);
});

it('should update search source correctly when useNewFieldsApi set to true', function () {
const searchSource = updateSearchSource(
savedSearchMock.searchSource,
'id',
[],
true,
indexPatternMock
);
const searchRequestBody = searchSource.getSearchRequestBody();
expect(searchRequestBody._source).toBe(false);
expect(searchRequestBody.track_total_hits).toBe(false);
});

it('should reject with an error when no hits were found', function () {
searchSourceStub._stubHits = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ISearchSource,
IndexPatternsContract,
EsQuerySortValue,
IndexPattern,
} from '../../../../../../data/public';
import { EsHitRecord } from './context';

Expand All @@ -27,31 +28,12 @@ export function fetchAnchorProvider(
sort: EsQuerySortValue[]
): Promise<EsHitRecord> {
const indexPattern = await indexPatterns.get(indexPatternId);
searchSource
.setParent(undefined)
.setField('index', indexPattern)
.setField('version', true)
.setField('size', 1)
.setField('query', {
query: {
constant_score: {
filter: {
ids: {
values: [anchorId],
},
},
},
},
language: 'lucene',
})
.setField('sort', sort);
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
searchSource.setField('fields', [{ field: '*', include_unmapped: 'true' }]);
}
updateSearchSource(searchSource, anchorId, sort, useNewFieldsApi, indexPattern);

const response = await searchSource.fetch();
const doc = get(response, ['hits', 'hits', 0]);

if (get(response, ['hits', 'total'], 0) < 1) {
if (!doc) {
throw new Error(
i18n.translate('discover.context.failedToLoadAnchorDocumentErrorDescription', {
defaultMessage: 'Failed to load anchor document.',
Expand All @@ -60,8 +42,41 @@ export function fetchAnchorProvider(
}

return {
...get(response, ['hits', 'hits', 0]),
...doc,
isAnchor: true,
} as EsHitRecord;
};
}

export function updateSearchSource(
searchSource: ISearchSource,
anchorId: string,
sort: EsQuerySortValue[],
useNewFieldsApi: boolean,
indexPattern: IndexPattern
) {
searchSource
.setParent(undefined)
.setField('index', indexPattern)
.setField('version', true)
.setField('size', 1)
.setField('query', {
query: {
constant_score: {
filter: {
ids: {
values: [anchorId],
},
},
},
},
language: 'lucene',
})
.setField('sort', sort)
.setField('trackTotalHits', false);
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
searchSource.setField('fields', [{ field: '*', include_unmapped: 'true' }]);
}
return searchSource;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Timestamp {
lte?: string;
}

describe('context app', function () {
describe('context predecessors', function () {
let fetchPredecessors: (
indexPatternId: string,
timeField: string,
Expand All @@ -49,7 +49,7 @@ describe('context app', function () {
data: {
search: {
searchSource: {
create: jest.fn().mockImplementation(() => mockSearchSource),
createEmpty: jest.fn().mockImplementation(() => mockSearchSource),
},
},
},
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('context app', function () {
data: {
search: {
searchSource: {
create: jest.fn().mockImplementation(() => mockSearchSource),
createEmpty: jest.fn().mockImplementation(() => mockSearchSource),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Timestamp {
lte?: string;
}

describe('context app', function () {
describe('context successors', function () {
let fetchSuccessors: (
indexPatternId: string,
timeField: string,
Expand All @@ -49,7 +49,7 @@ describe('context app', function () {
data: {
search: {
searchSource: {
create: jest.fn().mockImplementation(() => mockSearchSource),
createEmpty: jest.fn().mockImplementation(() => mockSearchSource),
},
},
},
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('context app', function () {
data: {
search: {
searchSource: {
create: jest.fn().mockImplementation(() => mockSearchSource),
createEmpty: jest.fn().mockImplementation(() => mockSearchSource),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { updateSearchSource } from './context';
import { indexPatternMock } from '../../../../__mocks__/index_pattern';
import { createSearchSourceMock } from '../../../../../../data/public/mocks';

describe('context api', function () {
test('createSearchSource when useFieldsApi is true', () => {
const newSearchSource = createSearchSourceMock({ index: indexPatternMock });
const searchSource = updateSearchSource(newSearchSource, indexPatternMock, [], true);
expect(searchSource.getSearchRequestBody()).toMatchSnapshot();
});
test('createSearchSource when useFieldsApi is false', () => {
const newSearchSource = createSearchSourceMock({ index: indexPatternMock });
const searchSource = updateSearchSource(newSearchSource, indexPatternMock, [], false);
expect(searchSource.getSearchRequestBody()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { estypes } from '@elastic/elasticsearch';
import { Filter, IndexPatternsContract, IndexPattern } from 'src/plugins/data/public';
import { Filter, IndexPatternsContract, IndexPattern, SearchSource } from 'src/plugins/data/public';
import { reverseSortDir, SortDirection } from './utils/sorting';
import { extractNanos, convertIsoToMillis } from './utils/date_conversion';
import { fetchHitsInInterval } from './utils/fetch_hits_in_interval';
Expand Down Expand Up @@ -46,7 +46,7 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
*
* @param {SurrDocType} type - `successors` or `predecessors`
* @param {string} indexPatternId
* @param {AnchorHitRecord} anchor - anchor record
* @param {EsHitRecord} anchor - anchor record
* @param {string} timeField - name of the timefield, that's sorted on
* @param {string} tieBreakerField - name of the tie breaker, the 2nd sort field
* @param {SortDirection} sortDir - direction of sorting
Expand All @@ -68,7 +68,9 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
return [];
}
const indexPattern = await indexPatterns.get(indexPatternId);
const searchSource = await createSearchSource(indexPattern, filters);
const { data } = getServices();
const searchSource = data.search.searchSource.createEmpty() as SearchSource;
updateSearchSource(searchSource, indexPattern, filters, Boolean(useNewFieldsApi));
const sortDirToApply = type === SurrDocType.SUCCESSORS ? sortDir : reverseSortDir(sortDir);

const nanos = indexPattern.isTimeNanosBased() ? extractNanos(anchor.fields[timeField][0]) : '';
Expand Down Expand Up @@ -116,20 +118,23 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields

return documents;
}
}

async function createSearchSource(indexPattern: IndexPattern, filters: Filter[]) {
const { data } = getServices();

const searchSource = await data.search.searchSource.create();
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
searchSource.setField('fields', [{ field: '*', include_unmapped: 'true' }]);
}
return searchSource
.setParent(undefined)
.setField('index', indexPattern)
.setField('filter', filters);
export function updateSearchSource(
searchSource: SearchSource,
indexPattern: IndexPattern,
filters: Filter[],
useNewFieldsApi: boolean
) {
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
searchSource.setField('fields', [{ field: '*', include_unmapped: 'true' }]);
}
return searchSource
.setParent(undefined)
.setField('index', indexPattern)
.setField('filter', filters)
.setField('trackTotalHits', false);
}

export { fetchContextProvider };

0 comments on commit dfa1e83

Please sign in to comment.