From 8d20c307e6021a3be9cf69acc85788380c6fffe0 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 26 Sep 2019 12:39:09 -0600 Subject: [PATCH 1/2] SearchSource: fix docvalue_fields and fields intersection logic --- .../ui/public/courier/search_source/search_source.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 ceb10861dc9a06..427c347f43b579 100644 --- a/src/legacy/ui/public/courier/search_source/search_source.js +++ b/src/legacy/ui/public/courier/search_source/search_source.js @@ -551,12 +551,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 +571,9 @@ 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 = flatData.body.docvalue_fields.filter(docValue => { + return fields.includes(docValue.field); + }); flatData.body.script_fields = _.pick(flatData.body.script_fields, fields); // request the remaining fields from both stored_fields and _source From 31e1f6f0b2342c353d3c86feee92cb5d8294306e Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 30 Sep 2019 06:37:48 -0600 Subject: [PATCH 2/2] 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 | 5 ++- 3 files changed, 60 insertions(+), 3 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 00000000000000..cd726709b4b5c2 --- /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 00000000000000..b220361e33b3ba --- /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 427c347f43b579..afa42a7d7c0154 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', @@ -571,9 +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 = flatData.body.docvalue_fields.filter(docValue => { - return fields.includes(docValue.field); - }); + 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