Skip to content
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

Merged
merged 20 commits into from
Feb 15, 2021

Conversation

majagrubic
Copy link
Contributor

@majagrubic majagrubic commented Feb 1, 2021

Summary

Fixes: #89837

This PR fixes the issue in the current where unmapped fields are not retrieved when retrieving fields from source.

It also adds source filter functionality to fields API, by explicitly requesting a list of fields from index pattern that are NOT excluded by the field filters.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@majagrubic
Copy link
Contributor Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

⏳ Build in-progress, with failures

Failed CI Steps

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@majagrubic majagrubic changed the title [Search Source] Remove includes when retrieving fields from source [Search Source] Fix retrieval of unmapped fields; Add field filters Feb 3, 2021
@@ -539,7 +538,7 @@ export class SearchSource {
if (!body.hasOwnProperty('_source')) {
body._source = sourceFilters;
}
if (body._source.excludes) {
if (body._source && body._source.excludes) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the check for body._source is needed? indexPatterns.getSourceFiltering() will always return something, even if it's just { excludes: [] }, so if _source isn't previously set it will already be set in L539.

Comment on lines 583 to 588
if (body._source && body._source.excludes) {
// TODO: optimize complexity
remainingFields = remainingFields.filter((remainingField) => {
return !body._source.excludes.includes(remainingField);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have fieldWildcardFilter above which does this, maybe that can be reused, or this filtering can be done earlier on?

Or actually: It seems like if we know we always want to filter items from excludes, we should filter body.script_fields and body.runtime_mappings as early as possible, so those values are never there to begin with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but we'd still need to explicitly exclude fieldsFromSource, right? So we'd end up with the same result basically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but we'd still need to explicitly exclude fieldsFromSource, right?

Yeah, I guess what I'm getting at is: If remainingFields is determined by comparing uniqFieldNames with script_fields and runtime_mappings, and uniqFieldNames includes fields and fieldsFromSource, we could probably do this filtering a bit earlier on in one go by filtering fields, fieldsFromSource, script_fields, and runtime_mappings. Then you wouldn't have to worry about using any of those items in the rest of the function body because you'd know they are already filtered.

Not a huge concern or anything, just felt like it might make this method (marginally) more readable.

Comment on lines 18 to 20
const fieldsToInclude = fields.filter((field) => {
return !sourceFiltersValues.includes(field.name) && !META_FIELDS.includes(field.name);
});
Copy link
Member

Choose a reason for hiding this comment

The 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 fieldsWildcardFilter.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this particular use case is supposed to take care of field filters when using the fields API. Unlike _source, fields API has no notion of excludes, but rather - includes. So we'd have to request a list of all fields, except the ones specified by the filters, which is what this code does. The logic doesn't exist (or doesn't work 😅 ) in search_source yet. TBF, I am not sure where the right place for it is. It felt cleaner to be in Discover, but happy to discuss alternatives.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fields API has no notion of excludes, but rather - includes. So we'd have to request a list of all fields, except the ones specified by the filters, which is what this code does.

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 setField('fields', ['*']), then search source would do this internally. But let's see what @ppisljar thinks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some discussions on date_nanos handling, we have reached the conclusion that Discover would explicitly specify the date format for any date field when requesting fields (#90415 (comment)). I believe this logic belongs to the same place as requesting fields to include, so if we could now decide where that would be, it would be appreciated :)

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 👍

@spalger
Copy link
Contributor

spalger commented Feb 3, 2021

jenkins test this

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - This PR does a better job of removing duplicate fields from _source which is great, thanks.
code review, tested in chrome

const request = await searchSource.getSearchRequestBody();
expect(request.stored_fields).toEqual(['geometry', 'prop1']);
expect(request.docvalue_fields).toEqual(['prop1']);
expect(request._source).toEqual({
Copy link
Contributor

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.

{
        "_index": "geo_shapes",
        "_id": "4",
        "_score": 0,
        "_source": {
          "prop1": 4,
          "geometry": { ...omitted }
        },
        "fields": {
          "prop1": [
            4
          ]
        }
      }

With this PR in place, the behavior is optimal and prop1's value is only returned a single time.

{
        "_index": "geo_shapes",
        "_id": "4",
        "_score": 0,
        "_source": {
          "geometry": {...omitted}
        },
        "fields": {
          "prop1": [
            4
          ]
        }
      }

@majagrubic
Copy link
Contributor Author

@elasticmachine merge upstream

@majagrubic majagrubic marked this pull request as ready for review February 8, 2021 12:14
@majagrubic majagrubic requested a review from a team February 8, 2021 12:14
@majagrubic majagrubic requested review from a team as code owners February 8, 2021 12:14
@majagrubic majagrubic added v7.12.0 release_note:roadmap Team:Visualizations Visualization editors, elastic-charts and infrastructure labels Feb 8, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

@majagrubic
Copy link
Contributor Author

@elasticmachine merge upstream

@@ -539,7 +595,7 @@ export class SearchSource {
if (!body.hasOwnProperty('_source')) {
body._source = sourceFilters;
}
if (body._source.excludes) {
if (body._source) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always evaluate to true and can probably be removed. (If body._source is undefined, we assign it on L596 above, and index.getSourceFiltering() will always be truthy).

@@ -614,6 +673,7 @@ export class SearchSource {
// if items that are in the docvalueFields are provided, we should
// inject the format from the computed fields if one isn't given
const docvaluesIndex = keyBy(filteredDocvalueFields, 'field');
body.fields = this.getFieldsWithoutSourceFilters(index, body.fields);
Copy link
Member

@lukeelmers lukeelmers Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run this, but I'm wondering why the logic for getFieldsWithoutSourceFilters wouldn't be colocated with the other source filtering stuff starting on L599. They are doing very similar things in applying filters to body.fields, and it seems could be combined (unless there's a reason we need the "unfiltered" values between L599 and here?)

[Edit] Apologies, just realized I'm repeating myself and already mentioned it in the thread above. Been a long week! 😬

Copy link
Contributor Author

@majagrubic majagrubic Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a long week for all of us, don't worry 😂 Thanks for the comments, will address this soon.

Copy link
Member

@lukeelmers lukeelmers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for incorporating feedback & braving the murky waters of search source @majagrubic!

@majagrubic
Copy link
Contributor Author

@elasticmachine merge upstream

Copy link
Member

@ppisljar ppisljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and thanks a lot @majagrubic for making this work!

@majagrubic
Copy link
Contributor Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
data 614 701 +87

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
data 236.5KB 236.5KB +2.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
data 798.7KB 904.6KB +105.9KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Member

@kertal kertal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM 👍 🙇 for this fix. Testing Chrome, Safari, Firefox, I think I've encountered one hopefully final issue:

I've excluded category.keyword, it's excluded in the doc viewer, but selectable in the sidebar
Bildschirmfoto 2021-02-15 um 10 41 04

One add-on, I think the intro text when adding the field filter is no longer valid:

Bildschirmfoto 2021-02-15 um 10 32 27

@majagrubic majagrubic merged commit 5b5f47f into elastic:master Feb 15, 2021
@majagrubic majagrubic deleted the inclusion-filters branch February 15, 2021 12:53
sorenlouv pushed a commit to sorenlouv/kibana that referenced this pull request Feb 15, 2021
…lastic#89837)

* [Search Source] Remove includes when retrieving fields from source

* Removing unusded imports

* Use exclusion filters to retrieve a list of fields

* Exclude _source from fields list

* Fix small check in getting the field list

* Fixing faulty import

* Filter logic

* Adding a unit test for maps use case

* Updating maps unit & functional test

* Add unit test

* Move logic for requesting a field list inside search_source

* Remove unnecessary mock

* Code cleanup as per PR comments

Co-authored-by: Kibana Machine <[email protected]>
jloleysens added a commit to jloleysens/kibana that referenced this pull request Feb 15, 2021
…ndition-for-hiding-recommded-allocation

* 'master' of github.com:elastic/kibana:
  [Discover] Fix toggling multi fields from doc view table (elastic#91121)
  [ML] Data Frame Analytics: ROC Curve Chart (elastic#89991)
  skip flaky suite (elastic#86948)
  skip flaky suite (elastic#91191)
  Fix date histogram time zone for rollup index (elastic#90632)
  [Search Source] Fix retrieval of unmapped fields; Add field filters (elastic#89837)
  [Logs UI] Use useMlHref hook for ML links (elastic#90935)
  Fix values of `products.min_price` field in Kibana sample ecommerce data set (elastic#90428)
  [APM] Darker shade for Error group details labels (elastic#91349)
  [Lens] Adjust new copy for 7.12 (elastic#90413)
  [ML] Unskip test. Fix modelMemoryLimit value. (elastic#91280)
  [Lens] Fix empty display name issue in XY chart (elastic#91132)
  [Lens] Improves error messages when in Dashboard (elastic#90668)
  [Lens] Keyboard-selected items follow user traversal of drop zones (elastic#90546)
  [Lens] Improves ranking feature in Top values (elastic#90749)
  [ILM] Rollover min age tooltip and copy fixes (elastic#91110)

# Conflicts:
#	x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.test.ts
majagrubic pushed a commit that referenced this pull request Feb 15, 2021
…89837) (#91396)

* [Search Source] Remove includes when retrieving fields from source

* Removing unusded imports

* Use exclusion filters to retrieve a list of fields

* Exclude _source from fields list

* Fix small check in getting the field list

* Fixing faulty import

* Filter logic

* Adding a unit test for maps use case

* Updating maps unit & functional test

* Add unit test

* Move logic for requesting a field list inside search_source

* Remove unnecessary mock

* Code cleanup as per PR comments

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
@timroes timroes added release_note:enhancement Feature:Search Querying infrastructure in Kibana and removed release_note:roadmap labels Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Search Querying infrastructure in Kibana release_note:enhancement Team:Visualizations Visualization editors, elastic-charts and infrastructure v7.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants