123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- [[modules-scripting-native]]
- === Native (Java) Scripts
- Sometimes `groovy` and <<modules-scripting-expression, expression>> aren't enough. For those times you can
- implement a native script.
- The best way to implement a native script is to write a plugin and install it.
- 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 actual script you'll need to implement `NativeScriptFactory`
- to construct the script. The actual script will extend either
- `AbstractExecutableScript` or `AbstractSearchScript`. The second one is likely
- the most useful and has several helpful subclasses you can extend like
- `AbstractLongSearchScript`, `AbstractDoubleSearchScript`, and
- `AbstractFloatSearchScript`. Finally, your plugin should register the native
- script by declaring the `onModule(ScriptModule)` method.
- If you squashed the whole thing into one class it'd look like:
- [source,java]
- --------------------------------------------------
- public class MyNativeScriptPlugin extends Plugin {
- @Override
- public String name() {
- return "my-native-script";
- }
- @Override
- public String description() {
- return "my native script that does something great";
- }
- public void onModule(ScriptModule scriptModule) {
- scriptModule.registerScript("my_script", MyNativeScriptFactory.class);
- }
- public static class MyNativeScriptFactory implements NativeScriptFactory {
- @Override
- public ExecutableScript newScript(@Nullable Map<String, Object> params) {
- return new MyNativeScript();
- }
- @Override
- public boolean needsScores() {
- return false;
- }
- }
- public static class MyNativeScript extends AbstractFloatSearchScript {
- @Override
- public float runAsFloat() {
- float a = (float) source().get("a");
- float b = (float) source().get("b");
- return a * b;
- }
- }
- }
- --------------------------------------------------
- You can execute the script by specifying its `lang` as `native`, and the name
- of the script as the `id`:
- [source,js]
- --------------------------------------------------
- curl -XPOST localhost:9200/_search -d '{
- "query": {
- "function_score": {
- "query": {
- "match": {
- "body": "foo"
- }
- },
- "functions": [
- {
- "script_score": {
- "script": {
- "id": "my_script",
- "lang" : "native"
- }
- }
- }
- ]
- }
- }
- }'
- --------------------------------------------------
|