|
23 | 23 | import org.apache.lucene.expressions.SimpleBindings; |
24 | 24 | import org.apache.lucene.expressions.js.JavascriptCompiler; |
25 | 25 | import org.apache.lucene.expressions.js.VariableContext; |
| 26 | +import org.apache.lucene.index.LeafReaderContext; |
26 | 27 | import org.apache.lucene.queries.function.ValueSource; |
27 | 28 | import org.apache.lucene.queries.function.valuesource.DoubleConstValueSource; |
| 29 | +import org.apache.lucene.search.Scorer; |
28 | 30 | import org.apache.lucene.search.SortField; |
29 | 31 | import org.elasticsearch.SpecialPermission; |
30 | 32 | import org.elasticsearch.common.Nullable; |
|
39 | 41 | import org.elasticsearch.script.ClassPermission; |
40 | 42 | import org.elasticsearch.script.ExecutableScript; |
41 | 43 | import org.elasticsearch.script.FilterScript; |
| 44 | +import org.elasticsearch.script.ScoreScript; |
42 | 45 | import org.elasticsearch.script.ScriptContext; |
43 | 46 | import org.elasticsearch.script.ScriptEngine; |
44 | 47 | import org.elasticsearch.script.ScriptException; |
45 | 48 | import org.elasticsearch.script.SearchScript; |
46 | 49 | import org.elasticsearch.search.lookup.SearchLookup; |
47 | 50 |
|
| 51 | +import java.io.IOException; |
48 | 52 | import java.security.AccessControlContext; |
49 | 53 | import java.security.AccessController; |
50 | 54 | import java.security.PrivilegedAction; |
@@ -111,6 +115,9 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE |
111 | 115 | } else if (context.instanceClazz.equals(FilterScript.class)) { |
112 | 116 | FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p); |
113 | 117 | return context.factoryClazz.cast(factory); |
| 118 | + } else if (context.instanceClazz.equals(ScoreScript.class)) { |
| 119 | + ScoreScript.Factory factory = (p, lookup) -> newScoreScript(expr, lookup, p); |
| 120 | + return context.factoryClazz.cast(factory); |
114 | 121 | } |
115 | 122 | throw new IllegalArgumentException("expression engine does not know how to handle script context [" + context.name + "]"); |
116 | 123 | } |
@@ -260,6 +267,42 @@ public void setDocument(int docid) { |
260 | 267 | }; |
261 | 268 | }; |
262 | 269 | } |
| 270 | + |
| 271 | + private ScoreScript.LeafFactory newScoreScript(Expression expr, SearchLookup lookup, @Nullable Map<String, Object> vars) { |
| 272 | + SearchScript.LeafFactory searchLeafFactory = newSearchScript(expr, lookup, vars); |
| 273 | + return new ScoreScript.LeafFactory() { |
| 274 | + @Override |
| 275 | + public boolean needs_score() { |
| 276 | + return searchLeafFactory.needs_score(); |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + public ScoreScript newInstance(LeafReaderContext ctx) throws IOException { |
| 281 | + SearchScript script = searchLeafFactory.newInstance(ctx); |
| 282 | + return new ScoreScript(vars, lookup, ctx) { |
| 283 | + @Override |
| 284 | + public double execute() { |
| 285 | + return script.runAsDouble(); |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public void setDocument(int docid) { |
| 290 | + script.setDocument(docid); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public void setScorer(Scorer scorer) { |
| 295 | + script.setScorer(scorer); |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public double get_score() { |
| 300 | + return script.getScore(); |
| 301 | + } |
| 302 | + }; |
| 303 | + } |
| 304 | + }; |
| 305 | + } |
263 | 306 |
|
264 | 307 | /** |
265 | 308 | * converts a ParseException at compile-time or link-time to a ScriptException |
|
0 commit comments