Skip to content

Conversation

@nreese
Copy link
Contributor

@nreese nreese commented Mar 27, 2023

Fixes #153595

Background

When advanced setting courier:ignoreFilterIfFieldNotInIndex is enabled, filters are ignored when data view does not contain the filtering field. The logic on how this works is captured below for context.

https://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/from_filters.ts#L83

      .filter((filter) => {
        const indexPattern = findIndexPattern(inputDataViews, filter.meta?.index);
        return !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern);
      })

https://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/filter_matches_index.ts#L20

export function filterMatchesIndex(filter: Filter, indexPattern?: DataViewBase | null) {
  if (!filter.meta?.key || !indexPattern) {
    return true;
  }

  // Fixes https://github.com/elastic/kibana/issues/89878
  // Custom filters may refer multiple fields. Validate the index id only.
  if (filter.meta?.type === 'custom') {
    return filter.meta.index === indexPattern.id;
  }

  return indexPattern.fields.some((field) => field.name === filter.meta.key);
}

The problem is that mapSpatialFilter is setting meta.key property. This causes filterMatchesIndex check to fail for spatial filters.

Spatial filters are designed to work across data views and avoid the problems that courier:ignoreFilterIfFieldNotInIndex solves. As such, spatial filters should not set meta.key property and not get removed when courier:ignoreFilterIfFieldNotInIndex is enabled.

Test

}

// ignore results for outdated requests
if (!_.isEqual(nextIndexPatternIds, this._prevIndexPatternIds)) {
Copy link
Contributor Author

@nreese nreese Mar 27, 2023

Choose a reason for hiding this comment

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

This is an unrelated fix for an issue where timing of getIndexPatterns response are out of order, resulting in setting state to a stale value.

@nreese
Copy link
Contributor Author

nreese commented Mar 28, 2023

@elasticmachine merge upstream

@nreese
Copy link
Contributor Author

nreese commented Mar 28, 2023

@elasticmachine merge upstream

@nreese nreese marked this pull request as ready for review March 28, 2023 17:29
@nreese nreese requested review from a team as code owners March 28, 2023 17:29
@nreese nreese added Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas t// auto-backport Deprecated - use backport:version if exact versions are needed Feature:Maps labels Mar 28, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-presentation (Team:Presentation)

Copy link
Member

@nickpeihl nickpeihl left a comment

Choose a reason for hiding this comment

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

lgtm! code review and tested with and without courier:ignoreFilterIfFieldNotInIndex enabled.

It is not possible to specify a data view using the Edit filter option in the filter pill context menu, however this is an issue in main and not related to this PR.

@nreese
Copy link
Contributor Author

nreese commented Mar 29, 2023

@elasticmachine merge upstream

@kibana-ci
Copy link

💚 Build Succeeded

Metrics [docs]

Async chunks

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

id before after diff
maps 2.7MB 2.7MB -213.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 403.8KB 403.6KB -192.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
securitySolution 432 435 +3

Total ESLint disabled count

id before after diff
securitySolution 512 515 +3

History

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

Copy link
Contributor

@stratoula stratoula left a comment

Choose a reason for hiding this comment

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

Changes LGTM!

@nreese nreese merged commit 4504cd1 into elastic:main Mar 31, 2023
kibanamachine added a commit that referenced this pull request Mar 31, 2023
…phic filters no longer working (#153816)

Fixes #153595

### Background

When advanced setting `courier:ignoreFilterIfFieldNotInIndex` is
enabled, filters are ignored when data view does not contain the
filtering field. The logic on how this works is captured below for
context.

https://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/from_filters.ts#L83
```
      .filter((filter) => {
        const indexPattern = findIndexPattern(inputDataViews, filter.meta?.index);
        return !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern);
      })
```

https://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/filter_matches_index.ts#L20
```
export function filterMatchesIndex(filter: Filter, indexPattern?: DataViewBase | null) {
  if (!filter.meta?.key || !indexPattern) {
    return true;
  }

  // Fixes #89878
  // Custom filters may refer multiple fields. Validate the index id only.
  if (filter.meta?.type === 'custom') {
    return filter.meta.index === indexPattern.id;
  }

  return indexPattern.fields.some((field) => field.name === filter.meta.key);
}
```

The problem is that
[mapSpatialFilter](https://github.com/elastic/kibana/blob/8.7/src/plugins/data/public/query/filter_manager/lib/mappers/map_spatial_filter.ts#L12)
is setting `meta.key` property. This causes `filterMatchesIndex` check
to fail for spatial filters.

Spatial filters are designed to work across data views and avoid the
problems that `courier:ignoreFilterIfFieldNotInIndex` solves. As such,
spatial filters should not set `meta.key` property and not get removed
when `courier:ignoreFilterIfFieldNotInIndex` is enabled.

### Test
* set advanced setting `courier:ignoreFilterIfFieldNotInIndex` and
refresh browser
* install 2 or more sample data sets
* create a new map
* add documents layer from one sample data set
* add documents layer from another sample data set
* create spatial filter on map,
https://www.elastic.co/guide/en/kibana/8.6/maps-create-filter-from-map.html#maps-spatial-filters
* Verify filter is applied to both layers

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 4504cd1)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.7

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

kibanamachine added a commit that referenced this pull request Mar 31, 2023
…geographic filters no longer working (#153816) (#154162)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[maps] fix When courier:ignoreFilterIfFieldNotInIndex enabled,
geographic filters no longer working
(#153816)](#153816)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nathan
Reese","email":"reese.nathan@elastic.co"},"sourceCommit":{"committedDate":"2023-03-31T13:59:01Z","message":"[maps]
fix When courier:ignoreFilterIfFieldNotInIndex enabled, geographic
filters no longer working (#153816)\n\nFixes
https://github.com/elastic/kibana/issues/153595\r\n\r\n###
Background\r\n\r\nWhen advanced setting
`courier:ignoreFilterIfFieldNotInIndex` is\r\nenabled, filters are
ignored when data view does not contain the\r\nfiltering field. The
logic on how this works is captured below
for\r\ncontext.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/from_filters.ts#L83\r\n```\r\n
.filter((filter) => {\r\n const indexPattern =
findIndexPattern(inputDataViews, filter.meta?.index);\r\n return
!ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter,
indexPattern);\r\n
})\r\n```\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/filter_matches_index.ts#L20\r\n```\r\nexport
function filterMatchesIndex(filter: Filter, indexPattern?: DataViewBase
| null) {\r\n if (!filter.meta?.key || !indexPattern) {\r\n return
true;\r\n }\r\n\r\n // Fixes
https://github.com/elastic/kibana/issues/89878\r\n // Custom filters may
refer multiple fields. Validate the index id only.\r\n if
(filter.meta?.type === 'custom') {\r\n return filter.meta.index ===
indexPattern.id;\r\n }\r\n\r\n return indexPattern.fields.some((field)
=> field.name === filter.meta.key);\r\n}\r\n```\r\n\r\nThe problem is
that\r\n[mapSpatialFilter](https://github.com/elastic/kibana/blob/8.7/src/plugins/data/public/query/filter_manager/lib/mappers/map_spatial_filter.ts#L12)\r\nis
setting `meta.key` property. This causes `filterMatchesIndex`
check\r\nto fail for spatial filters.\r\n\r\nSpatial filters are
designed to work across data views and avoid the\r\nproblems that
`courier:ignoreFilterIfFieldNotInIndex` solves. As such,\r\nspatial
filters should not set `meta.key` property and not get removed\r\nwhen
`courier:ignoreFilterIfFieldNotInIndex` is enabled.\r\n\r\n### Test\r\n*
set advanced setting `courier:ignoreFilterIfFieldNotInIndex`
and\r\nrefresh browser\r\n* install 2 or more sample data sets\r\n*
create a new map\r\n* add documents layer from one sample data set\r\n*
add documents layer from another sample data set\r\n* create spatial
filter on
map,\r\nhttps://www.elastic.co/guide/en/kibana/8.6/maps-create-filter-from-map.html#maps-spatial-filters\r\n*
Verify filter is applied to both
layers\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4504cd18c07c0f0af94830558f7906dd77a2b217","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Presentation","auto-backport","Feature:Maps","v8.8.0","v8.7.1"],"number":153816,"url":"https://github.com/elastic/kibana/pull/153816","mergeCommit":{"message":"[maps]
fix When courier:ignoreFilterIfFieldNotInIndex enabled, geographic
filters no longer working (#153816)\n\nFixes
https://github.com/elastic/kibana/issues/153595\r\n\r\n###
Background\r\n\r\nWhen advanced setting
`courier:ignoreFilterIfFieldNotInIndex` is\r\nenabled, filters are
ignored when data view does not contain the\r\nfiltering field. The
logic on how this works is captured below
for\r\ncontext.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/from_filters.ts#L83\r\n```\r\n
.filter((filter) => {\r\n const indexPattern =
findIndexPattern(inputDataViews, filter.meta?.index);\r\n return
!ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter,
indexPattern);\r\n
})\r\n```\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/filter_matches_index.ts#L20\r\n```\r\nexport
function filterMatchesIndex(filter: Filter, indexPattern?: DataViewBase
| null) {\r\n if (!filter.meta?.key || !indexPattern) {\r\n return
true;\r\n }\r\n\r\n // Fixes
https://github.com/elastic/kibana/issues/89878\r\n // Custom filters may
refer multiple fields. Validate the index id only.\r\n if
(filter.meta?.type === 'custom') {\r\n return filter.meta.index ===
indexPattern.id;\r\n }\r\n\r\n return indexPattern.fields.some((field)
=> field.name === filter.meta.key);\r\n}\r\n```\r\n\r\nThe problem is
that\r\n[mapSpatialFilter](https://github.com/elastic/kibana/blob/8.7/src/plugins/data/public/query/filter_manager/lib/mappers/map_spatial_filter.ts#L12)\r\nis
setting `meta.key` property. This causes `filterMatchesIndex`
check\r\nto fail for spatial filters.\r\n\r\nSpatial filters are
designed to work across data views and avoid the\r\nproblems that
`courier:ignoreFilterIfFieldNotInIndex` solves. As such,\r\nspatial
filters should not set `meta.key` property and not get removed\r\nwhen
`courier:ignoreFilterIfFieldNotInIndex` is enabled.\r\n\r\n### Test\r\n*
set advanced setting `courier:ignoreFilterIfFieldNotInIndex`
and\r\nrefresh browser\r\n* install 2 or more sample data sets\r\n*
create a new map\r\n* add documents layer from one sample data set\r\n*
add documents layer from another sample data set\r\n* create spatial
filter on
map,\r\nhttps://www.elastic.co/guide/en/kibana/8.6/maps-create-filter-from-map.html#maps-spatial-filters\r\n*
Verify filter is applied to both
layers\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4504cd18c07c0f0af94830558f7906dd77a2b217"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/153816","number":153816,"mergeCommit":{"message":"[maps]
fix When courier:ignoreFilterIfFieldNotInIndex enabled, geographic
filters no longer working (#153816)\n\nFixes
https://github.com/elastic/kibana/issues/153595\r\n\r\n###
Background\r\n\r\nWhen advanced setting
`courier:ignoreFilterIfFieldNotInIndex` is\r\nenabled, filters are
ignored when data view does not contain the\r\nfiltering field. The
logic on how this works is captured below
for\r\ncontext.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/from_filters.ts#L83\r\n```\r\n
.filter((filter) => {\r\n const indexPattern =
findIndexPattern(inputDataViews, filter.meta?.index);\r\n return
!ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter,
indexPattern);\r\n
})\r\n```\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/main/packages/kbn-es-query/src/es_query/filter_matches_index.ts#L20\r\n```\r\nexport
function filterMatchesIndex(filter: Filter, indexPattern?: DataViewBase
| null) {\r\n if (!filter.meta?.key || !indexPattern) {\r\n return
true;\r\n }\r\n\r\n // Fixes
https://github.com/elastic/kibana/issues/89878\r\n // Custom filters may
refer multiple fields. Validate the index id only.\r\n if
(filter.meta?.type === 'custom') {\r\n return filter.meta.index ===
indexPattern.id;\r\n }\r\n\r\n return indexPattern.fields.some((field)
=> field.name === filter.meta.key);\r\n}\r\n```\r\n\r\nThe problem is
that\r\n[mapSpatialFilter](https://github.com/elastic/kibana/blob/8.7/src/plugins/data/public/query/filter_manager/lib/mappers/map_spatial_filter.ts#L12)\r\nis
setting `meta.key` property. This causes `filterMatchesIndex`
check\r\nto fail for spatial filters.\r\n\r\nSpatial filters are
designed to work across data views and avoid the\r\nproblems that
`courier:ignoreFilterIfFieldNotInIndex` solves. As such,\r\nspatial
filters should not set `meta.key` property and not get removed\r\nwhen
`courier:ignoreFilterIfFieldNotInIndex` is enabled.\r\n\r\n### Test\r\n*
set advanced setting `courier:ignoreFilterIfFieldNotInIndex`
and\r\nrefresh browser\r\n* install 2 or more sample data sets\r\n*
create a new map\r\n* add documents layer from one sample data set\r\n*
add documents layer from another sample data set\r\n* create spatial
filter on
map,\r\nhttps://www.elastic.co/guide/en/kibana/8.6/maps-create-filter-from-map.html#maps-spatial-filters\r\n*
Verify filter is applied to both
layers\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4504cd18c07c0f0af94830558f7906dd77a2b217"}},{"branch":"8.7","label":"v8.7.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nathan Reese <reese.nathan@elastic.co>
nreese added a commit that referenced this pull request Apr 4, 2023
…filter disappear from map (#154087)

Fixes #107044 and
#109340

### Background
In Maps, users can [create spatial
filters](https://www.elastic.co/guide/en/kibana/current/maps-create-filter-from-map.html).
The filter is added to the Filter bar.
<img width="300" alt="Screen Shot 2021-09-03 at 4 07 33 PM"
src="https://user-images.githubusercontent.com/373691/133142628-9b03dec2-69ee-4c7c-ac8d-841168c88a94.png">
There are 2 important properties about spatial filters created from a
Map:
* `filter.meta.type` is set to `spatial_filter`. Maps uses
`filter.meta.type` to identify spatial filters. Spatial filters are
displayed on the map to provide a visual reference of all applied
spatial filters.
* `filter.meta.isMultiIndex` is set to true since Elasticsearch Query
DSL for the filter is not tied to a single index pattern or field. The
Elasticsearch Query DSL is generated in such a way that the filter works
for all map layers, regardless of index pattern or spatial field. In the
screenshot above a single filter is filtering both layers, even though
they are from different index patterns and different spatial fields.
* Each layer identifies one or more geo_point or geo_shape fields that
is used to display features for that layer
* The Elasticsearch Query DSL combines these into a `bool.should` clause
where each field provides a `bool.must` clause ensuring the spatial
field exists and that the document satisfies the spatial filter. Any
document matching any of the `bool.must` clauses allows the bool.should
clause to produce a match.

### Solution

[Earlier solution](#111897)
proposed Sept 2021. This original effort was abandoned as priorities
shifted. I investigated re-opening original solution, which included a
filter operator registry. This solution is no longer appropriate with
`combined` filters.

Instead, this PR takes a less complex approach and when
`filter.meta.isMultiIndex`, the filter editor displays the following:
* Only displays "custom" editor. "Values" editor is not available
* "Edit as filter values" toggle button is not visible
* Data view select is not visible
* onSubmit does not change filter to "custom" unless DSL is edited

<img width="500" alt="Screen Shot 2023-03-30 at 10 33 58 AM"
src="https://user-images.githubusercontent.com/373691/228918145-61ddda8b-43a3-44e8-8316-a803473f5a36.png">

### Test instructions
* Install web logs sample data set and open "[Logs] Total Requests and
Bytes" map
* Click wrench and select "Draw bounds to filter data". Draw bounds and
create a filter.
* In the Filter bar, Click "Edit filter". 
* Change the label and click save. The filter should still be displayed
on the map and the filter pill should have a new label.


<strike>Note for testing, until
#153816 is merged, spatial filters
with a single geofield will not set `isMultiIndex` to true and will
still have the errors referenced in the issues. Once
#153816 is merged all spatial
filters will set isMultiIndex.</strike>

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Deprecated - use backport:version if exact versions are needed Feature:Maps release_note:fix Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas t// v8.7.1 v8.8.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When courier:ignoreFilterIfFieldNotInIndex enabled, geographic filters no longer working

7 participants