[Saved Search Embeddable] Do not set source field when reading fields from source (#109069)

* [Saved Search Embeddable] Do not set source if reading fields from source enabled

* Extract functionality to a helper function and added unit tests

* Fix unit test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Maja Grubic 2021-08-23 12:34:35 +02:00 committed by GitHub
parent 5c5e191364
commit 6711005db2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 20 deletions

View file

@ -0,0 +1,33 @@
/*
* 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 { createSearchSourceMock } from '../../../../../data/common/search/search_source/mocks';
import { updateSearchSource } from './update_search_source';
import { indexPatternMock } from '../../../__mocks__/index_pattern';
import { SortOrder } from '../../../saved_searches/types';
describe('updateSearchSource', () => {
const defaults = {
sampleSize: 50,
defaultSort: 'asc',
};
it('updates a given search source', async () => {
const searchSource = createSearchSourceMock({});
updateSearchSource(searchSource, indexPatternMock, [] as SortOrder[], false, defaults);
expect(searchSource.getField('fields')).toBe(undefined);
// does not explicitly request fieldsFromSource when not using fields API
expect(searchSource.getField('fieldsFromSource')).toBe(undefined);
});
it('updates a given search source with the usage of the new fields api', async () => {
const searchSource = createSearchSourceMock({});
updateSearchSource(searchSource, indexPatternMock, [] as SortOrder[], true, defaults);
expect(searchSource.getField('fields')).toEqual([{ field: '*', include_unmapped: 'true' }]);
expect(searchSource.getField('fieldsFromSource')).toBe(undefined);
});
});

View file

@ -0,0 +1,33 @@
/*
* 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 { IndexPattern, ISearchSource } from '../../../../../data/common';
import { getSortForSearchSource } from '../../apps/main/components/doc_table';
import { SortPairArr } from '../../apps/main/components/doc_table/lib/get_sort';
export const updateSearchSource = (
searchSource: ISearchSource,
indexPattern: IndexPattern | undefined,
sort: (SortPairArr[] & string[][]) | undefined,
useNewFieldsApi: boolean,
defaults: {
sampleSize: number;
defaultSort: string;
}
) => {
const { sampleSize, defaultSort } = defaults;
searchSource.setField('size', sampleSize);
searchSource.setField('sort', getSortForSearchSource(sort, indexPattern, defaultSort));
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
const fields: Record<string, string> = { field: '*', include_unmapped: 'true' };
searchSource.setField('fields', [fields]);
} else {
searchSource.removeField('fields');
}
};

View file

@ -42,8 +42,9 @@ import { handleSourceColumnState } from '../angular/helpers';
import { DiscoverGridProps } from '../components/discover_grid/discover_grid';
import { DiscoverGridSettings } from '../components/discover_grid/types';
import { DocTableProps } from '../apps/main/components/doc_table/doc_table_wrapper';
import { getDefaultSort, getSortForSearchSource } from '../apps/main/components/doc_table';
import { getDefaultSort } from '../apps/main/components/doc_table';
import { SortOrder } from '../apps/main/components/doc_table/components/table_header/helpers';
import { updateSearchSource } from './helpers/update_search_source';
export type SearchProps = Partial<DiscoverGridProps> &
Partial<DocTableProps> & {
@ -143,26 +144,16 @@ export class SavedSearchEmbeddable
if (this.abortController) this.abortController.abort();
this.abortController = new AbortController();
searchSource.setField('size', this.services.uiSettings.get(SAMPLE_SIZE_SETTING));
searchSource.setField(
'sort',
getSortForSearchSource(
this.searchProps!.sort,
updateSearchSource(
searchSource,
this.searchProps!.indexPattern,
this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING)
)
this.searchProps!.sort,
useNewFieldsApi,
{
sampleSize: this.services.uiSettings.get(SAMPLE_SIZE_SETTING),
defaultSort: this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING),
}
);
if (useNewFieldsApi) {
searchSource.removeField('fieldsFromSource');
const fields: Record<string, string> = { field: '*', include_unmapped: 'true' };
searchSource.setField('fields', [fields]);
} else {
searchSource.removeField('fields');
if (this.searchProps.indexPattern) {
const fieldNames = this.searchProps.indexPattern.fields.map((field) => field.name);
searchSource.setField('fieldsFromSource', fieldNames);
}
}
// Log request to inspector
this.inspectorAdapters.requests!.reset();