Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SearchSource interface #20334

Merged
merged 18 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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