Skip to content

Separate AbstractScriptFieldType from RuntimeField implementations #74768

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

Merged
merged 7 commits into from
Jul 6, 2021

Conversation

romseygeek
Copy link
Contributor

AbstractScriptFieldType extends MappedFieldType and also implements
RuntimeField. This means that the name() of a runtime field has to be the
same as the name() of its corresponding mapped field type. However, for
object fields we will want the nested child fields to have a specific short name,
while their MappedFieldType will need to have a name that includes the parent.

To make this distinction possible, this commit changes AbstractScriptFieldType
subclasses to return their own implementations of RuntimeField from their
type parsers instead of implementing the interface directly.

@romseygeek romseygeek added :Search Foundations/Mapping Index mappings, including merging and defining field types >refactoring v8.0.0 v7.15.0 labels Jun 30, 2021
@romseygeek romseygeek requested a review from javanna June 30, 2021 15:54
@romseygeek romseygeek self-assigned this Jun 30, 2021
@elasticmachine elasticmachine added the Team:Search Meta label for search team label Jun 30, 2021
@elasticmachine
Copy link
Collaborator

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

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

I left some initial comments, thanks for working on this

import java.util.Collection;
import java.util.Collections;

public abstract class ScriptRuntimeField implements RuntimeField {
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit on the fence on the naming, mostly because the object field is also going to hold a script but will not extend this class. Maybe this could be named LeafRuntimeField or something along those lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++ to LeafRuntimeField, the main use of this base class is to simplify returning a single-valued collection from asMappedFieldTypes() and 'Leaf' reinforces that.

import java.util.Collection;
import java.util.Collections;

public abstract class ScriptRuntimeField implements RuntimeField {
Copy link
Member

Choose a reason for hiding this comment

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

can you also add javadocs to explain what this class is intended for?


public abstract class ScriptRuntimeField implements RuntimeField {

protected final String name;
Copy link
Member

Choose a reason for hiding this comment

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

this one corresponds to the simple field name in ObjectMapper right? It is only the name of the leaf field, although it can still contain dots? I wonder if we should clearly document this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have object fields yet so there isn't a distinction here at the moment. Let's document it when we know precisely how the two different names will work?

Copy link
Member

Choose a reason for hiding this comment

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

good point, thanks

@@ -288,6 +288,6 @@ public void execute() {
}

private static IpScriptFieldType build(Script script) {
return new IpScriptFieldType("test", factory(script), script, emptyMap(), (builder, params) -> builder);
return new IpScriptFieldType("test", factory(script), script, emptyMap());
Copy link
Member

Choose a reason for hiding this comment

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

this change feels good.

@@ -217,16 +217,14 @@ public void testRuntimeSectionMerge() throws IOException {
merge(mapperService, mapping);
DoubleScriptFieldType field = (DoubleScriptFieldType)mapperService.fieldType("field");
assertEquals(NumberFieldMapper.NumberType.DOUBLE.typeName(), field.typeName());
LongScriptFieldType field2Updated = (LongScriptFieldType)mapperService.fieldType("field2");
assertSame(field2, field2Updated);
assertThat(mapperService.fieldType("field2"), instanceOf(LongScriptFieldType.class));
Copy link
Member

Choose a reason for hiding this comment

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

so, now the runtime field is the same instance but not the mapped field type, or?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, exactly.

Copy link
Member

Choose a reason for hiding this comment

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

which makes me wonder: do we need to return a new instance each time asMappedFieldType is called? Probably not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We probably could create fixed MappedFieldTypes at construction time and always return the same object, but this is only called when Mappings are re-parsed so I'm not sure it really makes a lot of difference.

Copy link
Member

Choose a reason for hiding this comment

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

it certainly complicates the test no? What do we do for field mappers? Don't we always expose the same field type, that is created at construction time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have pushed a change to built the field types up front.

Copy link
Member

@javanna javanna 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!

@romseygeek romseygeek added the auto-backport Automatically create backport pull requests when merged label Jul 6, 2021
@romseygeek romseygeek merged commit 5656513 into elastic:master Jul 6, 2021
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
7.x Commit could not be cherrypicked due to conflicts

To backport manually run:
backport --pr 74768

@romseygeek romseygeek deleted the runtime/split-mappedfieldtype branch July 6, 2021 12:20
romseygeek added a commit that referenced this pull request Jul 6, 2021
…74768)

AbstractScriptFieldType extends MappedFieldType and also implements
RuntimeField. This means that the name() of a runtime field has to be the
same as the name() of its corresponding mapped field type. However, for
object fields we will want the nested child fields to have a specific short name,
while their MappedFieldType will need to have a name that includes the parent.

To make this distinction possible, this commit changes AbstractScriptFieldType
subclasses to return their own implementations of RuntimeField from their
type parsers instead of implementing the interface directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged >refactoring :Search Foundations/Mapping Index mappings, including merging and defining field types Team:Search Meta label for search team v7.15.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants