2017-05-11 15:15:16 -04:00
|
|
|
[[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"]
|
|
|
|
--------------------------------------------------
|
2017-06-15 11:54:10 -04:00
|
|
|
include-tagged::{plugins-examples-dir}/script-expert-scoring/src/main/java/org/elasticsearch/example/expertscript/ExpertScriptPlugin.java[expert_engine]
|
2017-05-11 15:15:16 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
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": {
|
2017-06-09 11:29:25 -04:00
|
|
|
"source": "pure_df",
|
2017-05-11 15:15:16 -04:00
|
|
|
"lang" : "expert_scripts",
|
|
|
|
"params": {
|
|
|
|
"field": "body",
|
|
|
|
"term": "foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[skip:we don't have an expert script plugin installed to test this]
|