-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Search Source] Fix retrieval of unmapped fields; Add field filters #89837
Changes from 11 commits
e870118
a691322
5321c06
170cb50
08a25e6
892d18c
0788854
736d6a7
0364594
ff9cd2b
2872c36
f7c845e
21b011e
0bca218
259cf6b
4279ef8
c1aef7d
a9c9389
5b94504
01aa170
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,9 +59,10 @@ | |
*/ | ||
|
||
import { setWith } from '@elastic/safer-lodash-set'; | ||
majagrubic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { uniqueId, keyBy, pick, difference, omit, isObject, isFunction } from 'lodash'; | ||
import { uniqueId, keyBy, pick, difference, omit, isFunction, isEqual } from 'lodash'; | ||
import { map, switchMap, tap } from 'rxjs/operators'; | ||
import { defer, from } from 'rxjs'; | ||
import { isObject } from 'rxjs/internal-compatibility'; | ||
import { normalizeSortRequest } from './normalize_sort_request'; | ||
import { fieldWildcardFilter } from '../../../../kibana_utils/common'; | ||
import { IIndexPattern } from '../../index_patterns'; | ||
|
@@ -503,7 +504,6 @@ export class SearchSource { | |
private flatten() { | ||
const { getConfig } = this.dependencies; | ||
const searchRequest = this.mergeProps(); | ||
|
||
searchRequest.body = searchRequest.body || {}; | ||
const { body, index, query, filters, highlightAll } = searchRequest; | ||
searchRequest.indexType = this.getIndexType(index); | ||
|
@@ -539,7 +539,7 @@ export class SearchSource { | |
if (!body.hasOwnProperty('_source')) { | ||
body._source = sourceFilters; | ||
} | ||
if (body._source.excludes) { | ||
if (body._source) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always evaluate to |
||
const filter = fieldWildcardFilter( | ||
body._source.excludes, | ||
getConfig(UI_SETTINGS.META_FIELDS) | ||
|
@@ -579,17 +579,20 @@ export class SearchSource { | |
const remainingFields = difference(uniqFieldNames, [ | ||
...Object.keys(body.script_fields), | ||
...Object.keys(body.runtime_mappings), | ||
]).filter(Boolean); | ||
]).filter((remainingField) => { | ||
if (!remainingField) return false; | ||
if (!body._source || !body._source.excludes) return true; | ||
return !body._source.excludes.includes(remainingField); | ||
}); | ||
|
||
// only include unique values | ||
body.stored_fields = [...new Set(remainingFields)]; | ||
|
||
// only include unique values | ||
if (fieldsFromSource.length) { | ||
// include remaining fields in _source | ||
setWith(body, '_source.includes', remainingFields, (nsValue) => | ||
isObject(nsValue) ? {} : nsValue | ||
); | ||
|
||
if (!isEqual(remainingFields, fieldsFromSource)) { | ||
setWith(body, '_source.includes', remainingFields, (nsValue) => | ||
isObject(nsValue) ? {} : nsValue | ||
); | ||
} | ||
// if items that are in the docvalueFields are provided, we should | ||
// make sure those are added to the fields API unless they are | ||
// already set in docvalue_fields | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { IndexPattern } from '../../../../data/common/index_patterns/index_patterns'; | ||
|
||
const META_FIELDS = ['_type', '_source']; | ||
|
||
export function getFieldListFromIndexPattern( | ||
indexPattern: IndexPattern, | ||
showUnmappedFields?: boolean | ||
) { | ||
const { sourceFilters, fields } = indexPattern; | ||
if (!sourceFilters || sourceFilters.length === 0) { | ||
const allFields: Record<string, string> = { field: '*' }; | ||
if (showUnmappedFields) { | ||
allFields.include_unmapped = 'true'; | ||
} | ||
return [allFields]; | ||
} | ||
const sourceFiltersValues = sourceFilters.map((sourceFilter) => sourceFilter.value); | ||
const fieldsToInclude = fields.filter((field) => { | ||
return !sourceFiltersValues.includes(field.name) && !META_FIELDS.includes(field.name); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be overlooking something (and things might have changed since I last looked at this stuff), but I'm not sure if this part should be necessary. The idea with search source is that you provide the index pattern, and this stuff happens internally so you don't need to worry about it. The filtering (and meta fields handling) should already happen inside search source via As a consumer of search source, all you should be responsible for is providing a field list, and search source would ideally handle everything else. But I might be oversimplifying: Is this addressing a case that is not already covered by search source? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this particular use case is supposed to take care of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, this is the point I missed. Yeah this does ultimately feel like something search source should handle. I would expect that if you called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some discussions on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current functionality was that when not setting the fields we would get you all of them. We changed this on the searchSource level, so that we would no longer return unmapped ones when using the fieldsAPI. I think there is place for this logic to live in the searchsource, thus get to the old behaviour. We could also make this optional by exposing `showUnmappedFields' on the searchsource. it seems from the linked PR that we are handling date_nanos on searchsource level as well, which makes sense imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change this then so that the logic lives in search source 👍 |
||
return fieldsToInclude.map((field) => { | ||
const fieldToInclude: Record<string, string> = { field: field.name }; | ||
if (showUnmappedFields) { | ||
fieldToInclude.include_unmapped = 'true'; | ||
} | ||
return fieldToInclude; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for moving this into its own test case.
After inspection, the behavior in master branch, where
_source: { includes: ['geometry', 'prop1']}
is not optimal and this PR's behavior is better. It is safe to update the expect statement to_source: { 'geometry'}
. This PR does a better job of removing duplicate fields from _source which is great, thanks.The existing behavior in master branch results in hits like the below. Notice how prop1's value is returned twice, once in _source and once in fields.
With this PR in place, the behavior is optimal and prop1's value is only returned a single time.