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
Scripting: Replace advanced and native scripts with ScriptEngine docs #24603
Conversation
This commit documents how to write a `ScriptEngine` in order to use expert internal apis, such as using Lucene directly to find index term statistics. These documents prepare the way to remove both native scripts and IndexLookup. The example java code is actually compiled and tested under a new gradle subproject for example plugins. This change does not yet breakup jvm-example into the new examples dir, which should be done separately. relates elastic#19359 relates elastic#19966
@@ -38,7 +38,9 @@ | |||
/** | |||
* The extension for file scripts in this language. | |||
*/ | |||
String getExtension(); | |||
default String getExtension() { | |||
return getType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
@@ -76,6 +76,15 @@ List projects = [ | |||
'qa:wildfly' | |||
] | |||
|
|||
File examplePluginsDir = new File(rootProject.projectDir, 'plugins/examples') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just declare them like all the other plugins? This doesn't seem like it is better than having the plugin in :plugins:examples:advanced-scoring
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it is different from every other project because the path doesn't match the name, which I'd prefer not to do just to keep things consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this for two reasons:
- Otherwise all the places that list plugins (eg smoke-test-plugins) via the gradle project
:plugins
must add a bunch of logic we must copy every time we want to get all the plugins. - I have come around to hating having to list every plugin/module, as it is both cumbersome to change, as well as error prone because moving/creating a new module results in a cryptic error message from gradle until it is added (thinking that the project is standalone instead of our multi project build).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are both fine points. Is there any way to accomplish them without using the funny project name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the first point, no. It works without having to affect anything iterating the plugins precisely because it is not a subproject of :plugins
.
Honestly, I don't see it as that big of a deal. This is what settings.gradle is for, to control how the multi project build is laid out. There is nothing that says project's directories must match their directory layout. The only reason I didn't add a top level example-plugins
directory is I think the root directory already has far too many things in it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just make a directory called example-plugins
instead of keeping it in the plugins directory? I like that the names all line up. Exception for buildSrc, but buildSrc is known special and weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, i think the root project is already too crowded. I don't want to add more directories there, I want to remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I spaced out and didn't read the second half of your paragraph. I should probably sleep soon!
I really want the names to line up if I can have it. If i can't fine.
What about putting it as a subdirectory of docs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having it as a subdirectory of docs will be confusing, because this is compilable and testable code, not ascii docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really want the names to line up if I can have it.
The names already don't line up, see for example the core hack we have around the same spot in settings.gradle. I really dont' see why the directory name matching the project name matters. These aren't things we ever have to deal with, other than in gradle, and gradle doesn't care what the names are, only that there is a dir associated with each project. cd
ing into the plugins/examples
dir still allows running tests as normal.
@Override | ||
public void close() {} | ||
} | ||
// end::expert_engine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you should include LeafScript
in the tag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good enough tweak I will try to move these into LeafSearchScript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed adf95e6.
} | ||
-------------------------------------------------- | ||
// CONSOLE | ||
// TEST[skip:we don't have an expert script plugin installed to test this] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, we totally could though. We already install all the plugins, I think we'd just need to fix the loop we use for it and this'd be installed too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but I'd rather leave that as a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We install all the other plugins for these tests so I think this would be the PR to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Left a couple of minor comments.
if ("pure_df".equals(scriptSource)) { | ||
return p -> new SearchScript() { | ||
String field = p.get("field").toString(); | ||
String term = p.get("term").toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be nice to add a check for existence of these parameters for completeness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added checks in 1070eb3.
* Return the result as a long. This is used by aggregation scripts over long fields. | ||
*/ | ||
default long runAsLong() { | ||
return (long) runAsDouble(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am somewhat conflicted about this. On one side I like this simplicity, on the other side if these default implementation are ever invoked, it most likely means that the script is used in a wrong context or has not been optimized for some contexts. Maybe, we shouldn't provide a default implementation, but instead clearly document in which contexts runAsLong
, runAsDouble
and run
are called from and follow the same model as we do with search vs executable script and throw UnsupportedOperationException
if a wrong method is called. I think this will encourage the users to read the comments on the method and implement the right method for their use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I changed runAsLong to default to UnsupportedOperationException
and tweaked the docs a tiny bit. Also note that with contexts coming this will all change significantly, as each context will have its own dedicated interface, so no more of this shared interface trying to satisfy multiple use cases.
…#24603) This commit documents how to write a `ScriptEngine` in order to use expert internal apis, such as using Lucene directly to find index term statistics. These documents prepare the way to remove both native scripts and IndexLookup. The example java code is actually compiled and tested under a new gradle subproject for example plugins. This change does not yet breakup jvm-example into the new examples dir, which should be done separately. relates #19359 relates #19966
This commit documents how to write a
ScriptEngine
in order to useexpert internal apis, such as using Lucene directly to find index term
statistics. These documents prepare the way to remove both native
scripts and IndexLookup.
The example java code is actually compiled and tested under a new gradle
subproject for example plugins. This change does not yet breakup
jvm-example into the new examples dir, which should be done separately.
relates #19359
relates #19966