From e877f0ebee31f7d2e23f748758a31b2b4ab4f9e1 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Fri, 4 Oct 2019 12:49:11 -0600 Subject: [PATCH] SearchSource: fix docvalue_fields and fields intersection logic (#46724) * SearchSource: fix docvalue_fields and fields intersection logic * update filter logic to handle docvalue_fields that are just strings --- .../search_source/filter_docvalue_fields.js | 25 ++++++++++++++ .../filter_docvalue_fields.test.js | 33 +++++++++++++++++++ .../courier/search_source/search_source.js | 10 +++--- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/legacy/ui/public/courier/search_source/filter_docvalue_fields.js create mode 100644 src/legacy/ui/public/courier/search_source/filter_docvalue_fields.test.js diff --git a/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.js b/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.js new file mode 100644 index 0000000000000..cd726709b4b5c --- /dev/null +++ b/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.js @@ -0,0 +1,25 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function filterDocvalueFields(docvalueFields, fields) { + return docvalueFields.filter(docValue => { + const docvalueFieldName = typeof docValue === 'string' ? docValue : docValue.field; + return fields.includes(docvalueFieldName); + }); +} diff --git a/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.test.js b/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.test.js new file mode 100644 index 0000000000000..b220361e33b3b --- /dev/null +++ b/src/legacy/ui/public/courier/search_source/filter_docvalue_fields.test.js @@ -0,0 +1,33 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { filterDocvalueFields } from './filter_docvalue_fields'; + +test('Should exclude docvalue_fields that are not contained in fields', () => { + const docvalueFields = [ + 'my_ip_field', + { field: 'my_keyword_field' }, + { field: 'my_date_field', 'format': 'epoch_millis' } + ]; + const out = filterDocvalueFields(docvalueFields, ['my_ip_field', 'my_keyword_field']); + expect(out).toEqual([ + 'my_ip_field', + { field: 'my_keyword_field' }, + ]); +}); diff --git a/src/legacy/ui/public/courier/search_source/search_source.js b/src/legacy/ui/public/courier/search_source/search_source.js index 671bcf0be74b3..2ff4b6d574ca3 100644 --- a/src/legacy/ui/public/courier/search_source/search_source.js +++ b/src/legacy/ui/public/courier/search_source/search_source.js @@ -81,6 +81,7 @@ import { searchRequestQueue } from '../search_request_queue'; import { FetchSoonProvider } from '../fetch'; import { FieldWildcardProvider } from '../../field_wildcard'; import { getHighlightRequest } from '../../../../../plugins/data/common/field_formats'; +import { filterDocvalueFields } from './filter_docvalue_fields'; const FIELDS = [ 'type', @@ -551,12 +552,13 @@ export function SearchSourceProvider(Promise, Private, config) { flatData.body = flatData.body || {}; const computedFields = flatData.index.getComputedFields(); + flatData.body.stored_fields = computedFields.storedFields; flatData.body.script_fields = flatData.body.script_fields || {}; - flatData.body.docvalue_fields = flatData.body.docvalue_fields || []; - _.extend(flatData.body.script_fields, computedFields.scriptFields); - flatData.body.docvalue_fields = _.union(flatData.body.docvalue_fields, computedFields.docvalueFields); + + const defaultDocValueFields = computedFields.docvalueFields ? computedFields.docvalueFields : []; + flatData.body.docvalue_fields = flatData.body.docvalue_fields || defaultDocValueFields; if (flatData.body._source) { // exclude source fields for this index pattern specified by the user @@ -570,7 +572,7 @@ export function SearchSourceProvider(Promise, Private, config) { const fields = flatData.fields; if (fields) { // filter out the docvalue_fields, and script_fields to only include those that we are concerned with - flatData.body.docvalue_fields = _.intersection(flatData.body.docvalue_fields, fields); + flatData.body.docvalue_fields = filterDocvalueFields(flatData.body.docvalue_fields, fields); flatData.body.script_fields = _.pick(flatData.body.script_fields, fields); // request the remaining fields from both stored_fields and _source