From 55a5999bd14fc680f54a271df6287c2228150f5c Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Wed, 21 Nov 2018 10:27:26 +0100 Subject: [PATCH] Decuple the UI setting config from kbn-es-query package --- .../src/es_query/__tests__/build_es_query.js | 20 +++---- .../src/es_query/__tests__/decorate_query.js | 6 +- .../src/es_query/__tests__/from_kuery.js | 8 +-- .../src/es_query/__tests__/from_lucene.js | 8 +-- .../src/es_query/build_es_query.js | 55 +++++++++++-------- .../src/es_query/decorate_query.js | 9 ++- .../kbn-es-query/src/es_query/from_kuery.js | 3 +- .../kbn-es-query/src/es_query/from_lucene.js | 4 +- packages/kbn-es-query/src/es_query/index.js | 2 +- .../public/kbn_vis_types/request_handler.js | 9 ++- .../public/vis/timelion_request_handler.js | 12 ++-- .../vega/public/vega_request_handler.js | 11 ++-- src/ui/public/agg_types/buckets/filters.js | 4 +- .../courier/search_source/search_source.js | 9 ++- .../datavisualizer_controller.js | 3 +- 15 files changed, 85 insertions(+), 78 deletions(-) diff --git a/packages/kbn-es-query/src/es_query/__tests__/build_es_query.js b/packages/kbn-es-query/src/es_query/__tests__/build_es_query.js index 3510ef276fd80..81f04822a81a9 100644 --- a/packages/kbn-es-query/src/es_query/__tests__/build_es_query.js +++ b/packages/kbn-es-query/src/es_query/__tests__/build_es_query.js @@ -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(); @@ -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: [ @@ -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); }); diff --git a/packages/kbn-es-query/src/es_query/__tests__/decorate_query.js b/packages/kbn-es-query/src/es_query/__tests__/decorate_query.js index ee6223ba98c20..5b3e64bb229ad 100644 --- a/packages/kbn-es-query/src/es_query/__tests__/decorate_query.js +++ b/packages/kbn-es-query/src/es_query/__tests__/decorate_query.js @@ -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 } }); }); }); diff --git a/packages/kbn-es-query/src/es_query/__tests__/from_kuery.js b/packages/kbn-es-query/src/es_query/__tests__/from_kuery.js index 07010442ae734..a9b5b5c1f961f 100644 --- a/packages/kbn-es-query/src/es_query/__tests__/from_kuery.js +++ b/packages/kbn-es-query/src/es_query/__tests__/from_kuery.js @@ -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: [], @@ -49,7 +47,7 @@ describe('build query', function () { } ); - const result = buildQueryFromKuery(indexPattern, queries, configStub); + const result = buildQueryFromKuery(indexPattern, queries, true); expect(result.filter).to.eql(expectedESQueries); }); @@ -57,7 +55,7 @@ describe('build query', function () { 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./ ); }); diff --git a/packages/kbn-es-query/src/es_query/__tests__/from_lucene.js b/packages/kbn-es-query/src/es_query/__tests__/from_lucene.js index 9c82eb026467c..5da5075925dbe 100644 --- a/packages/kbn-es-query/src/es_query/__tests__/from_lucene.js +++ b/packages/kbn-es-query/src/es_query/__tests__/from_lucene.js @@ -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 () { @@ -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); }); @@ -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]); }); diff --git a/packages/kbn-es-query/src/es_query/build_es_query.js b/packages/kbn-es-query/src/es_query/build_es_query.js index 309881e02c11e..2b31c1cecc43d 100644 --- a/packages/kbn-es-query/src/es_query/build_es_query.js +++ b/packages/kbn-es-query/src/es_query/build_es_query.js @@ -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'); diff --git a/packages/kbn-es-query/src/es_query/decorate_query.js b/packages/kbn-es-query/src/es_query/decorate_query.js index 7116f4d89a247..ff7c3ae80c690 100644 --- a/packages/kbn-es-query/src/es_query/decorate_query.js +++ b/packages/kbn-es-query/src/es_query/decorate_query.js @@ -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; diff --git a/packages/kbn-es-query/src/es_query/from_kuery.js b/packages/kbn-es-query/src/es_query/from_kuery.js index 8e5c5e564ac4f..9706e82f9bad1 100644 --- a/packages/kbn-es-query/src/es_query/from_kuery.js +++ b/packages/kbn-es-query/src/es_query/from_kuery.js @@ -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 }); diff --git a/packages/kbn-es-query/src/es_query/from_lucene.js b/packages/kbn-es-query/src/es_query/from_lucene.js index 618abde99f47e..7d6df6060ba02 100644 --- a/packages/kbn-es-query/src/es_query/from_lucene.js +++ b/packages/kbn-es-query/src/es_query/from_lucene.js @@ -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 { diff --git a/packages/kbn-es-query/src/es_query/index.js b/packages/kbn-es-query/src/es_query/index.js index af6456a32d820..d4d03f70bfcff 100644 --- a/packages/kbn-es-query/src/es_query/index.js +++ b/packages/kbn-es-query/src/es_query/index.js @@ -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'; diff --git a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js index 6fbe8cbc050b5..810828f0885d4 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js @@ -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', @@ -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 }; diff --git a/src/core_plugins/timelion/public/vis/timelion_request_handler.js b/src/core_plugins/timelion/public/vis/timelion_request_handler.js index 63ce1ff44e458..c9faa3c4e2a7a 100644 --- a/src/core_plugins/timelion/public/vis/timelion_request_handler.js +++ b/src/core_plugins/timelion/public/vis/timelion_request_handler.js @@ -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' @@ -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, { diff --git a/src/core_plugins/vega/public/vega_request_handler.js b/src/core_plugins/vega/public/vega_request_handler.js index 323c94cc828de..07d2d870156f4 100644 --- a/src/core_plugins/vega/public/vega_request_handler.js +++ b/src/core_plugins/vega/public/vega_request_handler.js @@ -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); @@ -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(); } diff --git a/src/ui/public/agg_types/buckets/filters.js b/src/ui/public/agg_types/buckets/filters.js index b0b513974a243..a853c8af2d559 100644 --- a/src/ui/public/agg_types/buckets/filters.js +++ b/src/ui/public/agg_types/buckets/filters.js @@ -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); diff --git a/src/ui/public/courier/search_source/search_source.js b/src/ui/public/courier/search_source/search_source.js index 577323caa3719..9696603e6a855 100644 --- a/src/ui/public/courier/search_source/search_source.js +++ b/src/ui/public/courier/search_source/search_source.js @@ -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'; @@ -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); @@ -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(); diff --git a/x-pack/plugins/ml/public/datavisualizer/datavisualizer_controller.js b/x-pack/plugins/ml/public/datavisualizer/datavisualizer_controller.js index 4601e1adfa04d..48ea057218cac 100644 --- a/x-pack/plugins/ml/public/datavisualizer/datavisualizer_controller.js +++ b/x-pack/plugins/ml/public/datavisualizer/datavisualizer_controller.js @@ -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; }