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

Runtime fields to expose _source and stored fields like existing scripts #61775

Conversation

javanna
Copy link
Member

@javanna javanna commented Sep 1, 2020

This PR exposes stored fields to runtime fields (through params._fields) and replaces the current way to access _source (source['field']) in favour of the documented way for all existing scripts: params._source .

Relates to #59332

@javanna javanna added >enhancement :Search/Search Search-related issues that do not fall into other categories labels Sep 1, 2020
@javanna javanna requested a review from nik9000 September 1, 2020 10:49
@elasticmachine
Copy link
Collaborator

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

@elasticmachine elasticmachine added the Team:Search Meta label for search team label Sep 1, 2020
…fields

This commit exposes stored fields to runtime fields (through params._fields) and replaces the current way to access _source (source['field']) in favour of the documented way for all existing scripts: params._source .
@javanna javanna force-pushed the enhancement/runtime_fields_source_fields_access branch from c085680 to 261b92a Compare September 1, 2020 12:19
@@ -166,7 +166,7 @@ private static String painlessToLoadFromSource(String name, String type) {
return null;
}
StringBuilder b = new StringBuilder();
b.append("def v = source['").append(name).append("'];\n");
b.append("def v = params._source.").append(name).append(";\n");
Copy link
Member

Choose a reason for hiding this comment

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

@javanna and I talked about this over video - I think that this syntax dates back to when we used mvel and the only reason for us to stick with it is backwards compatibility. But that aggregation scripts didn't really support _source before so maybe it isn't worth adding it to runtime fields like this. But we'll talk with a few folks about it before deciding for sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

@stu-elastic @jdconrad @rjernst what should we do here? We do want to give access to _source and stored fields in runtime fields, but we are wondering what the direction is on the long run and what syntax we should use. With this PR we are aligning with what is already supported but maybe that is not a good idea? What are your thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Long-term we want all of these to be top-level parameters, so it's just _source, _fields, etc. This is possible now using getter methods and storing the appropriate values as part of the script during instance creation, but for most contexts it'll require additional deprecation. For runtime fields unless there are objections I would prefer to just start with these as top level variables.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, that means I will update this PR to expose source as _source (currently exposes as source) and stored fields as _fields (currently not exposed)

Copy link
Member Author

Choose a reason for hiding this comment

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

it may make sense to start the deprecation dance in 7.x for existing scripts, at least that would allow us to have the new syntax in the docs, cause otherwise scripts for runtime fields will look completely different from anything listed in the docs. Or is it acceptable that runtime fields have their own separate scripting syntax in the docs too?

Copy link
Member

Choose a reason for hiding this comment

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

I'd like to start that dance in 7.x, yes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to make sure with @rjernst that those are the expected names for exposing these things.

Copy link
Member

Choose a reason for hiding this comment

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

We want to completely change how source and docvalues are accessed in scripts (see #61388). In the meantime, though, I think we should use existing patterns for access, to make it easier on users writing scripts across different contexts. While there are several ways we have of accessing source currently, the most common is params._source, so I think we should use that until we have a universal fields API.

I think that this syntax dates back to when we used mvel

History lesson! In mvel, we had _source. When we added painless, we wanted to deter users from using slow access methods, like source and fields. So we temporarily put these under params. The intent was to eventually formalize a way to access source in a different way in the global namespace of the script. Params was a dynamic place we could stash them in the meantime. However, we never got around to it fixing it (though we are looking at it now with the universal fields API), and params._source has stuck all these years.

// accessing doc_values through through params._doc and params.doc is deprecated in all existing script contexts,
// it makes little sense to expose it deprecated for runtime fields
if (entry.getKey().equals("_doc") == false && entry.getKey().equals("doc") == false) {
params.put(entry.getKey(), entry.getValue());
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it'd be more clear to:

params.put("_source", leafSearchLookup.searchLookup());
params.put("_fields", leafSearchLookup.fields());

?

Copy link
Member Author

Choose a reason for hiding this comment

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

possibly too clear :D

Copy link
Member

@rjernst rjernst Sep 1, 2020

Choose a reason for hiding this comment

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

I would not rely on asMap(), but instead be explicit as Nik suggests.

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed, I already made this change

@javanna
Copy link
Member Author

javanna commented Sep 2, 2020

run elasticsearch-ci/packaging-sample-windows

@javanna javanna merged commit 0040a59 into elastic:feature/runtime_fields Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants