Scripting: Optimize to native script execution when using just `doc.score`, closes #952.
This commit is contained in:
parent
3bafba8904
commit
811f14465a
|
@ -41,7 +41,10 @@ public abstract class AbstractSearchScript extends AbstractExecutableScript impl
|
|||
|
||||
private float score = Float.NaN;
|
||||
|
||||
// helper methods
|
||||
/**
|
||||
* Returns the current score and only applicable when used as a scoring script in a custom score query!.
|
||||
* For other cases, use {@link #doc()} and get the score from it.
|
||||
*/
|
||||
protected final float score() {
|
||||
return score;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import org.elasticsearch.search.lookup.SearchLookup;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -75,6 +76,9 @@ public class ScriptService extends AbstractComponent {
|
|||
}
|
||||
this.scriptEngines = builder.build();
|
||||
|
||||
// put some default optimized scripts
|
||||
staticCache.put("doc.score", new CompiledScript("native", new DocScoreNativeScriptFactory()));
|
||||
|
||||
// compile static scripts
|
||||
File scriptsFile = new File(env.configFile(), "scripts");
|
||||
if (scriptsFile.exists()) {
|
||||
|
@ -182,4 +186,19 @@ public class ScriptService extends AbstractComponent {
|
|||
cache.clear();
|
||||
}
|
||||
|
||||
public static class DocScoreNativeScriptFactory implements NativeScriptFactory {
|
||||
@Override public ExecutableScript newScript(@Nullable Map<String, Object> params) {
|
||||
return new DocScoreSearchScript();
|
||||
}
|
||||
}
|
||||
|
||||
public static class DocScoreSearchScript extends AbstractFloatSearchScript {
|
||||
@Override public float runAsFloat() {
|
||||
try {
|
||||
return doc().score();
|
||||
} catch (IOException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue