Skip to content

Commit

Permalink
Refactor SearchSource interface (#20334) (#20447)
Browse files Browse the repository at this point in the history
* Removed the dynamically assigned type, query, filter, sort, highlight, highlightAll, aggs, from, searchAfter, size, source, version, and fields methods.
* The accessor interface now consists of getField and setField methods which throw errors if an unrecognized property name is provided, in addition to getFields, setFields, getOwnField, and getId methods.
* Linked-list interface now consists of setParent and getParent.
* Factory interface now consists of create, createCopy, and createChild.
* Removed the unused unused enable, disable, and addFilterPredicate, and the redundant toString (method only used internally) and extend method (superseded by createChild).
* Internally, renamed the _state property to _data and grouped methods by concern.
  • Loading branch information
cjcenizal authored Jul 3, 2018
1 parent 51c43ed commit bd04df6
Show file tree
Hide file tree
Showing 40 changed files with 601 additions and 636 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
export function createSearchSource(kbnApi, initialState, indexPattern, aggs, useTimeFilter, filters = []) {
const searchSource = new kbnApi.SearchSource(initialState);
// Do not not inherit from rootSearchSource to avoid picking up time and globals
searchSource.inherits(false);
searchSource.filter(() => {
searchSource.setParent(false);
searchSource.setField('filter', () => {
const activeFilters = [...filters];
if (useTimeFilter) {
activeFilters.push(kbnApi.timeFilter.createFilter(indexPattern));
}
return activeFilters;
});
searchSource.size(0);
searchSource.index(indexPattern);
searchSource.aggs(aggs);
searchSource.setField('size', 0);
searchSource.setField('index', indexPattern);
searchSource.setField('aggs', aggs);
return searchSource;
}
13 changes: 6 additions & 7 deletions src/core_plugins/kibana/public/context/api/__tests__/_stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ export function createSearchSourceStubProvider(hits, timeField) {
}),
};

searchSourceStub.filter = sinon.stub().returns(searchSourceStub);
searchSourceStub.inherits = sinon.stub().returns(searchSourceStub);
searchSourceStub.set = sinon.stub().returns(searchSourceStub);
searchSourceStub.get = sinon.spy(key => {
const previousSetCall = searchSourceStub.set.withArgs(key).lastCall;
searchSourceStub.setParent = sinon.stub().returns(searchSourceStub);
searchSourceStub.setField = sinon.stub().returns(searchSourceStub);
searchSourceStub.getField = sinon.spy(key => {
const previousSetCall = searchSourceStub.setField.withArgs(key).lastCall;
return previousSetCall ? previousSetCall.args[1] : null;
});
searchSourceStub.fetch = sinon.spy(() => {
const timeField = searchSourceStub._stubTimeField;
const lastQuery = searchSourceStub.set.withArgs('query').lastCall.args[1];
const lastQuery = searchSourceStub.setField.withArgs('query').lastCall.args[1];
const timeRange = lastQuery.query.constant_score.filter.range[timeField];
const lastSort = searchSourceStub.set.withArgs('sort').lastCall.args[1];
const lastSort = searchSourceStub.setField.withArgs('sort').lastCall.args[1];
const sortDirection = lastSort[0][timeField];
const sortFunction =
sortDirection === 'asc'
Expand Down
57 changes: 27 additions & 30 deletions src/core_plugins/kibana/public/context/api/__tests__/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ import { SearchSourceProvider } from 'ui/courier';

import { fetchAnchorProvider } from '../anchor';

function createSearchSourceStubProvider(hits) {
const searchSourceStub = {
_stubHits: hits,
};

searchSourceStub.setParent = sinon.stub().returns(searchSourceStub);
searchSourceStub.setField = sinon.stub().returns(searchSourceStub);
searchSourceStub.fetch = sinon.spy(() => Promise.resolve({
hits: {
hits: searchSourceStub._stubHits,
total: searchSourceStub._stubHits.length,
},
}));

return function SearchSourceStubProvider() {
return searchSourceStub;
};
}

describe('context app', function () {
beforeEach(ngMock.module('kibana'));
Expand Down Expand Up @@ -61,9 +79,9 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const inheritsSpy = searchSourceStub.inherits;
expect(inheritsSpy.calledOnce).to.be(true);
expect(inheritsSpy.firstCall.args[0]).to.eql(false);
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.calledOnce).to.be(true);
expect(setParentSpy.firstCall.args[0]).to.eql(false);
});
});

Expand All @@ -72,9 +90,8 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const setIndexSpy = searchSourceStub.set.withArgs('index');
expect(setIndexSpy.calledOnce).to.be(true);
expect(setIndexSpy.firstCall.args[1]).to.eql({ id: 'INDEX_PATTERN_ID' });
const setFieldSpy = searchSourceStub.setField;
expect(setFieldSpy.firstCall.args[1]).to.eql({ id: 'INDEX_PATTERN_ID' });
});
});

Expand All @@ -83,7 +100,7 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const setVersionSpy = searchSourceStub.set.withArgs('version');
const setVersionSpy = searchSourceStub.setField.withArgs('version');
expect(setVersionSpy.calledOnce).to.be(true);
expect(setVersionSpy.firstCall.args[1]).to.eql(true);
});
Expand All @@ -94,7 +111,7 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const setSizeSpy = searchSourceStub.set.withArgs('size');
const setSizeSpy = searchSourceStub.setField.withArgs('size');
expect(setSizeSpy.calledOnce).to.be(true);
expect(setSizeSpy.firstCall.args[1]).to.eql(1);
});
Expand All @@ -105,7 +122,7 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const setQuerySpy = searchSourceStub.set.withArgs('query');
const setQuerySpy = searchSourceStub.setField.withArgs('query');
expect(setQuerySpy.calledOnce).to.be(true);
expect(setQuerySpy.firstCall.args[1]).to.eql({
query: {
Expand All @@ -128,7 +145,7 @@ describe('context app', function () {

return fetchAnchor('INDEX_PATTERN_ID', 'doc', 'id', [{ '@timestamp': 'desc' }, { '_doc': 'asc' }])
.then(() => {
const setSortSpy = searchSourceStub.set.withArgs('sort');
const setSortSpy = searchSourceStub.setField.withArgs('sort');
expect(setSortSpy.calledOnce).to.be(true);
expect(setSortSpy.firstCall.args[1]).to.eql([
{ '@timestamp': 'desc' },
Expand Down Expand Up @@ -167,23 +184,3 @@ describe('context app', function () {
});
});
});

function createSearchSourceStubProvider(hits) {
const searchSourceStub = {
_stubHits: hits,
};

searchSourceStub.filter = sinon.stub().returns(searchSourceStub);
searchSourceStub.inherits = sinon.stub().returns(searchSourceStub);
searchSourceStub.set = sinon.stub().returns(searchSourceStub);
searchSourceStub.fetch = sinon.spy(() => Promise.resolve({
hits: {
hits: searchSourceStub._stubHits,
total: searchSourceStub._stubHits.length,
},
}));

return function SearchSourceStubProvider() {
return searchSourceStub;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('context app', function () {
[]
)
.then((hits) => {
const intervals = searchSourceStub.set.args
const intervals = searchSourceStub.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => _.get(query, ['constant_score', 'filter', 'range', '@timestamp']));

Expand Down Expand Up @@ -131,7 +131,7 @@ describe('context app', function () {
[]
)
.then((hits) => {
const intervals = searchSourceStub.set.args
const intervals = searchSourceStub.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => _.get(query, ['constant_score', 'filter', 'range', '@timestamp']));

Expand Down Expand Up @@ -177,9 +177,9 @@ describe('context app', function () {
[]
)
.then(() => {
const inheritsSpy = searchSourceStub.inherits;
expect(inheritsSpy.alwaysCalledWith(false)).to.be(true);
expect(inheritsSpy.called).to.be(true);
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(false)).to.be(true);
expect(setParentSpy.called).to.be(true);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('context app', function () {
[]
)
.then((hits) => {
const intervals = searchSourceStub.set.args
const intervals = searchSourceStub.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => _.get(query, ['constant_score', 'filter', 'range', '@timestamp']));

Expand Down Expand Up @@ -133,7 +133,7 @@ describe('context app', function () {
[]
)
.then((hits) => {
const intervals = searchSourceStub.set.args
const intervals = searchSourceStub.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => _.get(query, ['constant_score', 'filter', 'range', '@timestamp']));

Expand Down Expand Up @@ -179,9 +179,9 @@ describe('context app', function () {
[]
)
.then(() => {
const inheritsSpy = searchSourceStub.inherits;
expect(inheritsSpy.alwaysCalledWith(false)).to.be(true);
expect(inheritsSpy.called).to.be(true);
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(false)).to.be(true);
expect(setParentSpy.called).to.be(true);
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/core_plugins/kibana/public/context/api/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export function fetchAnchorProvider(indexPatterns, Private) {
) {
const indexPattern = await indexPatterns.get(indexPatternId);
const searchSource = new SearchSource()
.inherits(false)
.set('index', indexPattern)
.set('version', true)
.set('size', 1)
.set('query', {
.setParent(false)
.setField('index', indexPattern)
.setField('version', true)
.setField('size', 1)
.setField('query', {
query: {
constant_score: {
filter: {
Expand All @@ -49,7 +49,7 @@ export function fetchAnchorProvider(indexPatterns, Private) {
},
language: 'lucene',
})
.set('sort', sort);
.setField('sort', sort);

const response = await searchSource.fetch();

Expand Down
20 changes: 10 additions & 10 deletions src/core_plugins/kibana/public/context/api/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { reverseSortDirection } from './utils/sorting';
/**
* @typedef {Object} SearchSourceT
* @prop {function(): Promise<SearchResult>} fetch
* @prop {function(string, any): SearchSourceT} set
* @prop {function(any): SearchSourceT} inherits
* @prop {function(string, any): SearchSourceT} setField
* @prop {function(any): SearchSourceT} setParent
*/

/**
Expand Down Expand Up @@ -162,9 +162,9 @@ function fetchContextProvider(indexPatterns, Private) {
const indexPattern = await indexPatterns.get(indexPatternId);

return new SearchSource()
.inherits(false)
.set('index', indexPattern)
.set('filter', filters);
.setParent(false)
.setField('index', indexPattern)
.setField('filter', filters);
}

/**
Expand Down Expand Up @@ -209,8 +209,8 @@ function fetchContextProvider(indexPatterns, Private) {
};

const response = await searchSource
.set('size', maxCount)
.set('query', {
.setField('size', maxCount)
.setField('query', {
query: {
constant_score: {
filter: {
Expand All @@ -225,15 +225,15 @@ function fetchContextProvider(indexPatterns, Private) {
},
language: 'lucene'
})
.set('searchAfter', [
.setField('searchAfter', [
afterTimeValue !== null ? afterTimeValue : startTimeValue,
tieBreakerValue,
])
.set('sort', [
.setField('sort', [
{ [timeField]: timeSortDirection },
{ [tieBreakerField]: tieBreakerSortDirection },
])
.set('version', true)
.setField('version', true)
.fetch();

return response.hits ? response.hits.hits : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getSavedDashboardMock(config) {
title: 'my dashboard',
panelsJSON: '[]',
searchSource: {
getOwn: (param) => param
getOwnField: (param) => param
}
};
return Object.assign(defaults, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ export class DashboardStateManager {
*/
applyFilters(query, filters) {
this.appState.query = query;
this.savedDashboard.searchSource.set('query', query);
this.savedDashboard.searchSource.set('filter', filters);
this.savedDashboard.searchSource.setField('query', query);
this.savedDashboard.searchSource.setField('filter', filters);
this.saveState();
}

Expand Down
6 changes: 3 additions & 3 deletions src/core_plugins/kibana/public/dashboard/lib/filter_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class FilterUtils {
* both query filters and filter bar filters.
*/
static getDashboardFilters(dashboard) {
return dashboard.searchSource.getOwn('filter');
return dashboard.searchSource.getOwnField('filter');
}

/**
Expand All @@ -53,8 +53,8 @@ export class FilterUtils {
* @returns {QueryFilter}
*/
static getQueryFilterForDashboard(dashboard) {
if (dashboard.searchSource.getOwn('query')) {
return dashboard.searchSource.getOwn('query');
if (dashboard.searchSource.getOwnField('query')) {
return dashboard.searchSource.getOwnField('query');
}

const dashboardFilters = this.getDashboardFilters(dashboard);
Expand Down
Loading

0 comments on commit bd04df6

Please sign in to comment.