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

Scripting: Replace advanced and native scripts with ScriptEngine docs #24603

Merged
merged 5 commits into from May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 31 additions & 4 deletions core/src/main/java/org/elasticsearch/script/LeafSearchScript.java
Expand Up @@ -19,18 +19,30 @@

package org.elasticsearch.script;

import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.lucene.ScorerAware;

import java.util.Map;

/**
* A per-segment {@link SearchScript}.
*
* This is effectively a functional interface, requiring at least implementing {@link #runAsDouble()}.
*/
public interface LeafSearchScript extends ScorerAware, ExecutableScript {

void setDocument(int doc);
/**
* Set the document this script will process next.
*/
default void setDocument(int doc) {}

@Override
default void setScorer(Scorer scorer) {}

void setSource(Map<String, Object> source);
/**
* Set the source for the current document.
*/
default void setSource(Map<String, Object> source) {}

/**
* Sets per-document aggregation {@code _value}.
Expand All @@ -44,8 +56,23 @@ default void setNextAggregationValue(Object value) {
setNextVar("_value", value);
}

long runAsLong();
@Override
default void setNextVar(String field, Object value) {}

double runAsDouble();
/**
* Return the result as a long. This is used by aggregation scripts over long fields.
*/
default long runAsLong() {
throw new UnsupportedOperationException("runAsLong is not implemented");
}

@Override
default Object run() {
return runAsDouble();
}

/**
* Return the result as a double. This is the main use case of search script, used for document scoring.
*/
double runAsDouble();
}
Expand Up @@ -38,7 +38,9 @@ public interface ScriptEngine extends Closeable {
/**
* The extension for file scripts in this language.
*/
String getExtension();
default String getExtension() {
return getType();
Copy link
Member

Choose a reason for hiding this comment

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

+1

}

/**
* Compiles a script.
Expand Down
6 changes: 2 additions & 4 deletions docs/reference/modules/scripting.asciidoc
Expand Up @@ -51,7 +51,7 @@ certain tasks.
|built-in
|templates

|<<modules-scripting-native, `java`>>
|<<modules-scripting-engine, `java`>>
|n/a
|you write it!
|expert API
Expand Down Expand Up @@ -83,6 +83,4 @@ include::scripting/painless-debugging.asciidoc[]

include::scripting/expression.asciidoc[]

include::scripting/native.asciidoc[]

include::scripting/advanced-scripting.asciidoc[]
include::scripting/engine.asciidoc[]
189 changes: 0 additions & 189 deletions docs/reference/modules/scripting/advanced-scripting.asciidoc

This file was deleted.

57 changes: 57 additions & 0 deletions docs/reference/modules/scripting/engine.asciidoc
@@ -0,0 +1,57 @@
[[modules-scripting-engine]]
=== Advanced scripts using script engines

A `ScriptEngine` is a backend for implementing a scripting language. It may also
be used to write scripts that need to use advanced internals of scripting. For example,
a script that wants to use term frequencies while scoring.

The plugin {plugins}/plugin-authors.html[documentation] has more information on
how to write a plugin so that Elasticsearch will properly load it. To register
the `ScriptEngine`, your plugin should implement the `ScriptPlugin` interface
and override the `getScriptEngine(Settings settings)` method.

The following is an example of a custom `ScriptEngine` which uses the language
name `expert_scripts`. It implements a single script called `pure_df` which
may be used as a search script to override each document's score as
the document frequency of a provided term.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{docdir}/../../plugins/examples/script-expert-scoring/src/main/java/org/elasticsearch/example/expertscript/ExpertScriptPlugin.java[expert_engine]
--------------------------------------------------

You can execute the script by specifying its `lang` as `expert_scripts`, and the name
of the script as the the script source:


[source,js]
--------------------------------------------------
POST /_search
{
"query": {
"function_score": {
"query": {
"match": {
"body": "foo"
}
},
"functions": [
{
"script_score": {
"script": {
"inline": "pure_df",
"lang" : "expert_scripts",
"params": {
"field": "body",
"term": "foo"
}
}
}
}
]
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[skip:we don't have an expert script plugin installed to test this]
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.