Skip to content

Commit

Permalink
SearchSource: fix docvalue_fields and fields intersection logic (elas…
Browse files Browse the repository at this point in the history
…tic#46724)

* SearchSource: fix docvalue_fields and fields intersection logic

* update filter logic to handle docvalue_fields that are just strings
  • Loading branch information
nreese committed Oct 4, 2019
1 parent c1932e3 commit e877f0e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
}
Original file line number Diff line number Diff line change
@@ -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' },
]);
});
10 changes: 6 additions & 4 deletions src/legacy/ui/public/courier/search_source/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e877f0e

Please sign in to comment.