Skip to content

Commit

Permalink
Decuple the UI setting config from kbn-es-query package
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Nov 26, 2018
1 parent cc7070d commit 55a5999
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 78 deletions.
20 changes: 8 additions & 12 deletions packages/kbn-es-query/src/es_query/__tests__/build_es_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@
*/

import expect from 'expect.js';
import { BuildESQueryProvider } from '../build_es_query';
import { buildEsQuery } from '../build_es_query';
import indexPattern from '../../__fixtures__/index_pattern_response.json';
import { fromKueryExpression, toElasticsearchQuery } from '../../kuery';
import { luceneStringToDsl } from '../lucene_string_to_dsl';
import { decorateQuery } from '../decorate_query';

let buildEsQuery;

const configStub = { get: () => ({}) };
const privateStub = fn => fn(configStub);

describe('build query', function () {
describe('buildESQuery', function () {
beforeEach(() => {
buildEsQuery = privateStub(BuildESQueryProvider);
});
describe('buildEsQuery', function () {

it('should return the parameters of an Elasticsearch bool query', function () {
const result = buildEsQuery();
Expand All @@ -59,11 +51,15 @@ describe('build query', function () {
meta: { type: 'match_all' },
},
];
const config = {
allowLeadingWildcards: true,
queryStringOptions: {},
};

const expectedResult = {
bool: {
must: [
decorateQuery(luceneStringToDsl('bar:baz'), configStub),
decorateQuery(luceneStringToDsl('bar:baz'), config.queryStringOptions),
{ match_all: {} },
],
filter: [
Expand All @@ -74,7 +70,7 @@ describe('build query', function () {
}
};

const result = buildEsQuery(indexPattern, queries, filters);
const result = buildEsQuery(indexPattern, queries, filters, config);

expect(result).to.eql(expectedResult);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
import expect from 'expect.js';
import { decorateQuery } from '../decorate_query';

const configStub = {
get: () => ({ analyze_wildcard: true })
};

describe('Query decorator', function () {
it('should be a function', function () {
expect(decorateQuery).to.be.a(Function);
});

it('should merge in the query string options', function () {
const decoratedQuery = decorateQuery({ query_string: { query: '*' } }, configStub);
const decoratedQuery = decorateQuery({ query_string: { query: '*' } }, { analyze_wildcard: true });
expect(decoratedQuery).to.eql({ query_string: { query: '*', analyze_wildcard: true } });
});
});
8 changes: 3 additions & 5 deletions packages/kbn-es-query/src/es_query/__tests__/from_kuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import expect from 'expect.js';
import { fromKueryExpression, toElasticsearchQuery } from '../../kuery';

describe('build query', function () {
const configStub = { get: () => true };

describe('buildQueryFromKuery', function () {
it('should return the parameters of an Elasticsearch bool query', function () {
const result = buildQueryFromKuery(null, [], configStub);
const result = buildQueryFromKuery(null, [], true);
const expected = {
must: [],
filter: [],
Expand All @@ -49,15 +47,15 @@ describe('build query', function () {
}
);

const result = buildQueryFromKuery(indexPattern, queries, configStub);
const result = buildQueryFromKuery(indexPattern, queries, true);

expect(result.filter).to.eql(expectedESQueries);
});

it('should throw a useful error if it looks like query is using an old, unsupported syntax', function () {
const oldQuery = { query: 'is(foo, bar)', language: 'kuery' };

expect(buildQueryFromKuery).withArgs(indexPattern, [oldQuery], configStub).to.throwError(
expect(buildQueryFromKuery).withArgs(indexPattern, [oldQuery], true).to.throwError(
/It looks like you're using an outdated Kuery syntax./
);
});
Expand Down
8 changes: 3 additions & 5 deletions packages/kbn-es-query/src/es_query/__tests__/from_lucene.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { buildQueryFromLucene } from '../from_lucene';
import { decorateQuery } from '../decorate_query';
import { luceneStringToDsl } from '../lucene_string_to_dsl';

const configStub = { get: () => ({}) };

describe('build query', function () {

describe('buildQueryFromLucene', function () {
Expand All @@ -47,11 +45,11 @@ describe('build query', function () {

const expectedESQueries = queries.map(
(query) => {
return decorateQuery(luceneStringToDsl(query.query), configStub);
return decorateQuery(luceneStringToDsl(query.query), {});
}
);

const result = buildQueryFromLucene(queries, configStub);
const result = buildQueryFromLucene(queries, {});

expect(result.must).to.eql(expectedESQueries);
});
Expand All @@ -61,7 +59,7 @@ describe('build query', function () {
{ query: { match_all: {} }, language: 'lucene' },
];

const result = buildQueryFromLucene(queries, configStub);
const result = buildQueryFromLucene(queries, {});

expect(result.must).to.eql([queries[0].query]);
});
Expand Down
55 changes: 31 additions & 24 deletions packages/kbn-es-query/src/es_query/build_es_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,37 @@ import { buildQueryFromKuery } from './from_kuery';
import { buildQueryFromFilters } from './from_filters';
import { buildQueryFromLucene } from './from_lucene';

export function BuildESQueryProvider(config) {

/**
*
* @param queries - an array of query objects. Each query has a language property and a query property.
* @param filters - an array of filter objects
*/
function buildESQuery(indexPattern, queries = [], filters = []) {
const validQueries = queries.filter((query) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');

const kueryQuery = buildQueryFromKuery(indexPattern, queriesByLanguage.kuery, config);
const luceneQuery = buildQueryFromLucene(queriesByLanguage.lucene, config);
const filterQuery = buildQueryFromFilters(filters, indexPattern);
/**
* @param indexPattern
* @param queries - an array of query objects. Each query has a language property and a query property.
* @param filters - an array of filter objects
* @param config - an objects with query:allowLeadingWildcards and query:queryString:options UI
* settings in form of { allowLeadingWildcards, queryStringOptions }
*/
export function buildEsQuery(
indexPattern,
queries = [],
filters = [],
{
allowLeadingWildcards = false,
queryStringOptions = {},
}) {
const validQueries = queries.filter((query) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');

return {
bool: {
must: [].concat(kueryQuery.must, luceneQuery.must, filterQuery.must),
filter: [].concat(kueryQuery.filter, luceneQuery.filter, filterQuery.filter),
should: [].concat(kueryQuery.should, luceneQuery.should, filterQuery.should),
must_not: [].concat(kueryQuery.must_not, luceneQuery.must_not, filterQuery.must_not),
}
};
}
const kueryQuery = buildQueryFromKuery(indexPattern, queriesByLanguage.kuery, allowLeadingWildcards);
const luceneQuery = buildQueryFromLucene(queriesByLanguage.lucene, queryStringOptions);
const filterQuery = buildQueryFromFilters(filters, indexPattern);

return buildESQuery;
return {
bool: {
must: [].concat(kueryQuery.must, luceneQuery.must, filterQuery.must),
filter: [].concat(kueryQuery.filter, luceneQuery.filter, filterQuery.filter),
should: [].concat(kueryQuery.should, luceneQuery.should, filterQuery.should),
must_not: [].concat(kueryQuery.must_not, luceneQuery.must_not, filterQuery.must_not),
}
};
}

//const allowLeadingWildcards = config.get('query:allowLeadingWildcards');
//const queryOptions = config.get('query:queryString:options');
9 changes: 4 additions & 5 deletions packages/kbn-es-query/src/es_query/decorate_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import _ from 'lodash';

/**
* Decorate queries with default parameters
* @param {query} query object
* @param query object
* @param queryStringOptions query:queryString:options from UI settings
* @returns {object}
*/
export function decorateQuery(query, config) {
const queryOptions = config.get('query:queryString:options');

export function decorateQuery(query, queryStringOptions) {
if (_.has(query, 'query_string.query')) {
_.extend(query.query_string, queryOptions);
_.extend(query.query_string, queryStringOptions);
}

return query;
Expand Down
3 changes: 1 addition & 2 deletions packages/kbn-es-query/src/es_query/from_kuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

import { fromLegacyKueryExpression, fromKueryExpression, toElasticsearchQuery, nodeTypes } from '../kuery';

export function buildQueryFromKuery(indexPattern, queries = [], config) {
const allowLeadingWildcards = config.get('query:allowLeadingWildcards');
export function buildQueryFromKuery(indexPattern, queries = [], allowLeadingWildcards) {
const queryASTs = queries.map(query => {
try {
return fromKueryExpression(query.query, { allowLeadingWildcards });
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-es-query/src/es_query/from_lucene.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import _ from 'lodash';
import { decorateQuery } from './decorate_query';
import { luceneStringToDsl } from './lucene_string_to_dsl';

export function buildQueryFromLucene(queries, config) {
export function buildQueryFromLucene(queries, queryStringOptions) {
const combinedQueries = _.map(queries, (query) => {
const queryDsl = luceneStringToDsl(query.query);
return decorateQuery(queryDsl, config);
return decorateQuery(queryDsl, queryStringOptions);
});

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-es-query/src/es_query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

export { BuildESQueryProvider } from './build_es_query';
export { buildEsQuery } from './build_es_query';
export { buildQueryFromFilters } from './from_filters';
export { luceneStringToDsl } from './lucene_string_to_dsl';
export { migrateFilter } from './migrate_filter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import { validateInterval } from '../lib/validate_interval';
import { timezoneProvider } from 'ui/vis/lib/timezone';
import { timefilter } from 'ui/timefilter';
import { BuildESQueryProvider } from '@kbn/es-query';
import { buildEsQuery } from '@kbn/es-query';

const MetricsRequestHandlerProvider = function (Private, Notifier, config, $http, i18n) {
const notify = new Notifier({ location: i18n('tsvb.requestHandler.notifier.locationNameTitle', { defaultMessage: 'Metrics' }) });
const buildEsQuery = Private(BuildESQueryProvider);

return {
name: 'metrics',
Expand All @@ -36,10 +35,14 @@ const MetricsRequestHandlerProvider = function (Private, Notifier, config, $http
const parsedTimeRange = timefilter.calculateBounds(timeRange);
const scaledDataFormat = config.get('dateFormat:scaled');
const dateFormat = config.get('dateFormat');
const esQueryConfigs = {
allowLeadingWildcards: config.get('query:allowLeadingWildcards'),
queryStringOptions: config.get('query:queryString:options'),
};
if (panel && panel.id) {
const params = {
timerange: { timezone, ...parsedTimeRange },
filters: [buildEsQuery(undefined, [query], filters)],
filters: [buildEsQuery(undefined, [query], filters, esQueryConfigs)],
panels: [panel],
state: uiStateObj
};
Expand Down
12 changes: 7 additions & 5 deletions src/core_plugins/timelion/public/vis/timelion_request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
*/

import _ from 'lodash';
import { BuildESQueryProvider } from '@kbn/es-query';
import { buildEsQuery } from '@kbn/es-query';
import { timezoneProvider } from 'ui/vis/lib/timezone';

const TimelionRequestHandlerProvider = function (Private, Notifier, $http) {
const TimelionRequestHandlerProvider = function (Private, Notifier, $http, config) {
const timezone = Private(timezoneProvider)();
const buildEsQuery = Private(BuildESQueryProvider);

const notify = new Notifier({
location: 'Timelion'
Expand All @@ -36,12 +35,15 @@ const TimelionRequestHandlerProvider = function (Private, Notifier, $http) {
return new Promise((resolve, reject) => {
const expression = visParams.expression;
if (!expression) return;

const esQueryConfigs = {
allowLeadingWildcards: config.get('query:allowLeadingWildcards'),
queryStringOptions: config.get('query:queryString:options'),
};
const httpResult = $http.post('../api/timelion/run', {
sheet: [expression],
extended: {
es: {
filter: buildEsQuery(undefined, [query], filters)
filter: buildEsQuery(undefined, [query], filters, esQueryConfigs)
}
},
time: _.extend(timeRange, {
Expand Down
11 changes: 7 additions & 4 deletions src/core_plugins/vega/public/vega_request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import { VegaParser } from './data_model/vega_parser';
import { SearchCache } from './data_model/search_cache';
import { TimeCache } from './data_model/time_cache';
import { timefilter } from 'ui/timefilter';
import { BuildESQueryProvider } from '@kbn/es-query';
import { buildEsQuery } from '@kbn/es-query';

export function VegaRequestHandlerProvider(Private, es, serviceSettings) {
const buildEsQuery = Private(BuildESQueryProvider);
export function VegaRequestHandlerProvider(es, serviceSettings, config) {
const searchCache = new SearchCache(es, { max: 10, maxAge: 4 * 1000 });
const timeCache = new TimeCache(timefilter, 3 * 1000);

Expand All @@ -35,7 +34,11 @@ export function VegaRequestHandlerProvider(Private, es, serviceSettings) {

handler({ timeRange, filters, query, visParams }) {
timeCache.setTimeRange(timeRange);
const filtersDsl = buildEsQuery(undefined, [query], filters);
const esQueryConfigs = {
allowLeadingWildcards: config.get('query:allowLeadingWildcards'),
queryStringOptions: config.get('query:queryString:options'),
};
const filtersDsl = buildEsQuery(undefined, [query], filters, esQueryConfigs);
const vp = new VegaParser(visParams.spec, searchCache, timeCache, filtersDsl, serviceSettings);
return vp.parseAsync();
}
Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/agg_types/buckets/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export const filtersBucketAgg = new BucketAggType({
console.log('malformed filter agg params, missing "query" on input'); // eslint-disable-line no-console
return;
}
const config = chrome.getUiSettingsClient();
const queryStringOptions = config.get('query:queryString:options');

decorateQuery(query, chrome.getUiSettingsClient());
decorateQuery(query, queryStringOptions);

const matchAllLabel = (filter.input.query === '' && _.has(query, 'match_all')) ? '*' : '';
const label = filter.label || matchAllLabel || _.get(query, 'query_string.query') || angular.toJson(query);
Expand Down
9 changes: 6 additions & 3 deletions src/ui/public/courier/search_source/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

import _ from 'lodash';
import angular from 'angular';
import { BuildESQueryProvider } from '@kbn/es-query';
import { buildEsQuery } from '@kbn/es-query';

import '../../promises';
import { NormalizeSortRequestProvider } from './_normalize_sort_request';
Expand Down Expand Up @@ -120,7 +120,6 @@ export function SearchSourceProvider(Promise, Private, config) {
const SegmentedSearchRequest = Private(SegmentedSearchRequestProvider);
const normalizeSortRequest = Private(NormalizeSortRequestProvider);
const fetchSoon = Private(FetchSoonProvider);
const buildESQuery = Private(BuildESQueryProvider);
const { fieldWildcardFilter } = Private(FieldWildcardProvider);
const getConfig = (...args) => config.get(...args);

Expand Down Expand Up @@ -611,7 +610,11 @@ export function SearchSourceProvider(Promise, Private, config) {
}

try {
flatData.body.query = buildESQuery(flatData.index, flatData.query, flatData.filters);
const esQueryConfigs = {
allowLeadingWildcards: config.get('query:allowLeadingWildcards'),
queryStringOptions: config.get('query:queryString:options'),
};
flatData.body.query = buildEsQuery(flatData.index, flatData.query, flatData.filters, esQueryConfigs);
} catch (e) {
if (e.message === 'OutdatedKuerySyntaxError') {
throw new OutdatedKuerySyntaxError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ module

function buildSearchQuery() {
const searchQuery = luceneStringToDsl($scope.searchQueryText);
decorateQuery(searchQuery, config);
const queryStringOptions = config.get('query:queryString:options');
decorateQuery(searchQuery, queryStringOptions);
return searchQuery;
}

Expand Down

0 comments on commit 55a5999

Please sign in to comment.