You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Implementing custom scripting language in Elasticsearch [modules-scripting-engine]
12
12
13
-
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.
13
+
A `ScriptEngine` is a backend for implementing a scripting language in {{es}}.
14
14
15
-
The plugin [documentation](elasticsearch://extend/index.md) 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.
15
+
## How it works
16
16
17
-
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.
17
+
Custom script engines integrate with {{es}} scripting framework through the `ScriptEngine` interface. To register the `ScriptEngine`, your plugin should implement the `ScriptPlugin` interface and override the `getScriptEngine(Settings settings)` method during plugin initialization.
18
+
19
+
## When to implement
20
+
21
+
Consider implementing a custom script engine when you need to use advanced internals of scripting, such as scripts that require term frequencies while scoring, or when implementing specialized scripting languages with custom syntax beyond standard Painless capabilities.
22
+
23
+
## Example implementation
24
+
25
+
The plugin [documentation](elasticsearch://extend/index.md) has more information on how to write a plugin so {{es}} will properly load it. For the complete ScriptEngine interface reference, refer to the [official implementation](https://github.com/elastic/elasticsearch/blob/main/server/src/main/java/org/elasticsearch/script/ScriptEngine.java).
26
+
27
+
### What this script does
28
+
29
+
This code creates a custom script engine that allows you to use `expert_scripts` as the language name and `pure_df` as the script source in your {{es}} queries. The script calculates document scores using term frequency data instead of {{es}} standard scoring algorithm.
30
+
31
+
The following example shows the essential parts of implementing a custom `ScriptEngine`:
returnfalse; // Return true if the script needs the score
100
-
}
73
+
### Key points
101
74
102
-
@Override
103
-
publicbooleanneeds_termStats() {
104
-
returnfalse; // Return true if the script needs term statistics via get_termStats()
105
-
}
75
+
***Language Definition**: The `getType()` method returns `expert_scripts`, which becomes the value you use for the `lang` parameter in your scripts.
76
+
***Script Recognition:** The `compile()` method identifies `pure_df` as a valid script source, which becomes the value you use for the `source` parameter.
77
+
***Custom Scoring:** The `execute()` method replaces {{es}} standard scoring with your custom logic. In this case, using term frequency as the document score.
* the field and/or term don't exist in this segment,
114
-
* so always return 0
115
-
*/
116
-
returnnewScoreScript(params, lookup, docReader) {
117
-
@Override
118
-
publicdoubleexecute(
119
-
ExplanationHolderexplanation
120
-
) {
121
-
if(explanation !=null) {
122
-
explanation.set("An example optional custom description to explain details for this script's execution; we'll provide a default one if you leave this out.");
explanation.set("An example optional custom description to explain details for this script's execution; we'll provide a default one if you leave this out.");
149
-
}
150
-
if (postings.docID() != currentDocid) {
151
-
/*
152
-
* advance moved past the current doc, so this
153
-
* doc has no occurrences of the term
154
-
*/
155
-
return0.0d;
156
-
}
157
-
try {
158
-
return postings.freq();
159
-
} catch (IOException e) {
160
-
thrownewUncheckedIOException(e);
161
-
}
162
-
}
163
-
};
164
-
}
165
-
}
166
-
}
167
-
```
79
+
**For the complete implementation, refer to the [official script engine example](https://github.com/elastic/elasticsearch/blob/main/plugins/examples/script-expert-scoring/src/main/java/org/elasticsearch/example/expertscript/ExpertScriptPlugin.java).**
80
+
81
+
### Usage example
168
82
169
-
You can execute the script by specifying its `lang` as `expert_scripts`, and the name of the script as the script source:
83
+
This example shows how to use your custom script engine in a search query:
0 commit comments