Skip to content

Commit

Permalink
[Saved Search Embeddable] Do not set source field when reading fields…
Browse files Browse the repository at this point in the history
… from source (elastic#109069) (elastic#109612)

* [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 <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Maja Grubic and kibanamachine authored Aug 23, 2021
1 parent e818e98 commit 3dbe474
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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> & {
Expand Down Expand Up @@ -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,
this.searchProps!.indexPattern,
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);
updateSearchSource(
searchSource,
this.searchProps!.indexPattern,
this.searchProps!.sort,
useNewFieldsApi,
{
sampleSize: this.services.uiSettings.get(SAMPLE_SIZE_SETTING),
defaultSort: this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING),
}
}
);

// Log request to inspector
this.inspectorAdapters.requests!.reset();
Expand Down

0 comments on commit 3dbe474

Please sign in to comment.