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

Merge V2 index/component template mappings in specific manner #55607

Merged
merged 1 commit into from
Apr 22, 2020

Conversation

dakrone
Copy link
Member

@dakrone dakrone commented Apr 22, 2020

This commit changes the way that V2 index, component, and request mappings are merged. Specifically:

  • Fields are merged in a "replacement" manner, meaning that the entire definition is replaced rather
    than merging the interior configuration
  • Mapping metadata (all fields outside of properties) are merged recursively.

The merging for V1 templates does not change.

Relates to #53101

This commit changes the way that V2 index, component, and request mappings are merged. Specifically:

- Fields are merged in a "replacement" manner, meaning that the entire definition is replaced rather
than merging the interior configuration
- Mapping metadata (all fields outside of `properties`) are merged recursively.

The merging for V1 templates does not change.

Relates to elastic#53101
@dakrone dakrone added :Data Management/Indices APIs APIs to create and manage indices and templates v8.0.0 v7.8.0 labels Apr 22, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features (:Core/Features/Indices APIs)

@martijnvg martijnvg mentioned this pull request Apr 22, 2020
39 tasks
Copy link
Contributor

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

Left only a minor suggestion

Comment on lines +569 to +570
Map<String, Object> innerTemplateNonProperties = new HashMap<>(innerTemplateMapping);
Map<String, Object> maybeProperties = (Map<String, Object>) innerTemplateNonProperties.remove("properties");
Copy link
Contributor

Choose a reason for hiding this comment

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

innerTemplateMapping is only used to initialize innerTemplateNonProperties

Would it make sense for these statements to be executed in reversed order to avoid over allocating innerTemplateNonProperties just to immediately remove "properties" ?

ie.

Map<String, Object> maybeProperties = (Map<String, Object>) innerTemplateMapping.remove("properties");
Map<String, Object> innerTemplateNonProperties = new HashMap<>(innerTemplateMapping);

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, ignore this actually, as we'd be modifying a map we didn't create. It's fine as it is

@dakrone
Copy link
Member Author

dakrone commented Apr 22, 2020

(Some builds were aborted due to Jenkins restart)

@elasticmachine run elasticsearch-ci/bwc
@elasticmachine run elasticsearch-ci/oss-distro-docs
@elasticmachine run elasticsearch-ci/packaging-sample-matrix-windows
@elasticmachine run elasticsearch-ci/packaging-sample-unix-archives
@elasticmachine run elasticsearch-ci/packaging-sample-unix-docker
@elasticmachine run elasticsearch-ci/packaging-sample-unix-packages

@dakrone dakrone merged commit 4574802 into elastic:master Apr 22, 2020
@dakrone dakrone deleted the itv2-merge-mappings-more-better branch April 22, 2020 18:51
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Apr 22, 2020
…c#55607)

This commit changes the way that V2 index, component, and request mappings are merged. Specifically:

- Fields are merged in a "replacement" manner, meaning that the entire definition is replaced rather
than merging the interior configuration
- Mapping metadata (all fields outside of `properties`) are merged recursively.

The merging for V1 templates does not change.

Relates to elastic#53101
dakrone added a commit that referenced this pull request Apr 22, 2020
…55607) (#55619)

This commit changes the way that V2 index, component, and request mappings are merged. Specifically:

- Fields are merged in a "replacement" manner, meaning that the entire definition is replaced rather
than merging the interior configuration
- Mapping metadata (all fields outside of `properties`) are merged recursively.

The merging for V1 templates does not change.

Relates to #53101
@jtibshirani jtibshirani added the :Search Foundations/Mapping Index mappings, including merging and defining field types label Apr 22, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/Mapping)

Copy link
Contributor

@jtibshirani jtibshirani left a comment

Choose a reason for hiding this comment

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

This caught my eye as I was looking at open PRs. The current behavior is a bit unexpected to me -- we only replace the top-level keys, as opposed to the field definitions. For example, my understanding is that merging these two mappings

{
  "properties": {
    "object": {
      "foo": { "type": "keyword" }
    }
}
{
  "properties": {
    "object": {
      "bar": { "type": "keyword" }
    }
}

will currently result in

{
  "properties": {
    "object": {
      "bar": { "type": "keyword" }
    }
}

Is this the intended behavior? It seems like we would want to be able to introduce new fields, even if they shared a parent object with an existing field.

I'm also wondering if we want to merge all the non-properties blocks recursively. For example in #29200 we discussed that we'd like dynamic templates to replace existing ones with the same name.

@jtibshirani
Copy link
Contributor

jtibshirani commented Apr 24, 2020

@jpountz had a good observation about dots in field names. Given a low-priority template with an object field object, and a high-priority template with the key object.field, the object field does not get replaced. Instead you end up with two top-level keys, object and object.field.

To me this produces pretty subtle behavior -- sometimes top-level fields are overwritten, sometimes they are not.

dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 4, 2020
This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.
dakrone added a commit that referenced this pull request Jun 8, 2020
)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in #55607 and #55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: #57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <[email protected]>
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 8, 2020
…stic#57701)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <[email protected]>
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 8, 2020
…stic#57701)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Data Management/Indices APIs APIs to create and manage indices and templates >enhancement :Search Foundations/Mapping Index mappings, including merging and defining field types v7.8.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants