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

Apply runtime mappings directly to MappingLookup #73572

Conversation

javanna
Copy link
Member

@javanna javanna commented May 31, 2021

SearchExecutionContext currently holds the runtime mappings and each field type lookup, or calls to getMatchingFieldNames or getMatchingFieldTypes, needs to have the runtime mappings applied first, as runtime mappings have the precedence when they have the same name as fields that come from the index mappings (runtime or not).

This caused code duplication, as the logic in those three methods is exactly the same that we already have in FieldTypeLookup. Also, we recently enhanced FieldTypeLookup to apply dynamic field types provided with the runtime section, and the same needs to be then also applied to SearchExecutionContext so that it supports dynamic runtime fields as well.

To address this problem, SearchExecutionContext has now access to a copy of MappingLookup with the runtime mappings applied to it, which is exactly the same MappingLookup as the original one, besides the FieldTypeLookup which is a copy of the original one with the runtime mappings applied on top.

@javanna javanna added >enhancement :Search Foundations/Mapping Index mappings, including merging and defining field types v8.0.0 v7.14.0 labels May 31, 2021
@javanna javanna requested review from nik9000 and jtibshirani May 31, 2021 19:06
@elasticmachine elasticmachine added the Team:Search Meta label for search team label May 31, 2021
@elasticmachine
Copy link
Collaborator

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

this.indexTimeScriptMappers = mappingLookup.indexTimeScriptMappers;
this.fieldMappers = mappingLookup.fieldMappers;
this.objectMappers = mappingLookup.objectMappers;
this.fieldTypeLookup = new FieldTypeLookup(mappingLookup.fieldTypeLookup, runtimeMappings);
Copy link
Member Author

@javanna javanna May 31, 2021

Choose a reason for hiding this comment

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

This is one way to achieve what we want (not duplicating code). I am happy with it code-wise. I do wonder if it's too heavy to make a copy of MappingLookup and FieldTypeLookup on each shard. I could not find a way to save work and share more between shards, even if we know that the runtime mappings will be the same on all shards for the same search request, because we parse them on the shards, hence we apply them there after parsing.

If we are worried about this, we could take a different approach, perhaps less elegant: avoid making copies of these two objects and rather expose new methods on them that allow to overlay runtime fields on the fly, a bit like search execution used to previously do. I am not too sure how this would look but I thought that it would not be ideal code-wise. Another idea I considered was using a search FieldTypeLookup within SearchExecutionContext: lightweight but in would introduce a new abstraction in SearchExecutionContext, would require to make FieldTypeLookup public etc.

I am curious to know what you think!

@javanna javanna force-pushed the enhancement/search_exec_context_runtime_mappings branch from 84713aa to 1f4b299 Compare June 1, 2021 07:56
SearchExecutionContext currently holds the runtime mappings and each field type lookup, or calls to getMatchingFieldNames or getMatchingFieldTypes, needs to have the runtime mappings applied first, as runtime mappings have the precedence when they have the same name as fields that come from the index mappings (runtime or not).

This caused code duplication, as the logic in those three methods is exactly the same that we already have in FieldTypeLookup. Also, we recently enhanced FieldTypeLookup to apply dynamic field types provided with the runtime section, and the same needs to be then also applied to SearchExecutionContext so that it supports dynamic runtime fields as well.

To address this problem, SearchExecutionContext has now access to a copy of MappingLookup with the runtime mappings applied to it, which is exactly the same MappingLookup as the original one, besides the FieldTypeLookup which is a copy of the original one with the runtime mappings applied on top.
@javanna javanna force-pushed the enhancement/search_exec_context_runtime_mappings branch from 1f4b299 to 244f736 Compare June 1, 2021 08:54
@javanna
Copy link
Member Author

javanna commented Jun 1, 2021

heads up I am investigating alternatives: I started with #73590 and I am looking into decreasing getMatchingFieldTypes usages or something along those lines, in the hope that we only have to apply the runtime mappings logic to getMatchingFieldNames and getFieldType. That may affect the approach we take trying to avoid code duplication. Copying all of the field type lookup does not seem like a great trade-off.

@javanna
Copy link
Member Author

javanna commented Jun 4, 2021

Closing this, we will take a different and less invasive approach. We were able to already reduce the code duplication between FieldTypeLookup and SearchExecutionContext to one method instead of two: getMatchingFieldNames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search Foundations/Mapping Index mappings, including merging and defining field types Team:Search Meta label for search team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants