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
This commit is contained in:
Nathan Reese 2019-10-04 12:49:11 -06:00 committed by GitHub
parent 9c85bd364d
commit 3e9b0c81b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 4 deletions

View file

@ -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);
});
}

View file

@ -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' },
]);
});

View file

@ -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