2929import org .elasticsearch .script .javascript .support .NativeList ;
3030import org .elasticsearch .script .javascript .support .NativeMap ;
3131import org .elasticsearch .script .javascript .support .ScriptValueConverter ;
32+ import org .elasticsearch .search .lookup .LeafSearchLookup ;
3233import org .elasticsearch .search .lookup .SearchLookup ;
3334import org .mozilla .javascript .*;
3435
36+ import java .io .IOException ;
3537import java .util .List ;
3638import java .util .Map ;
3739import java .util .concurrent .atomic .AtomicLong ;
@@ -121,26 +123,33 @@ public ExecutableScript executable(Object compiledScript, Map<String, Object> va
121123 }
122124
123125 @ Override
124- public SearchScript search (Object compiledScript , SearchLookup lookup , @ Nullable Map <String , Object > vars ) {
126+ public SearchScript search (final Object compiledScript , final SearchLookup lookup , @ Nullable final Map <String , Object > vars ) {
125127 Context ctx = Context .enter ();
126128 try {
127129 ctx .setWrapFactory (wrapFactory );
128130
129- Scriptable scope = ctx .newObject (globalScope );
131+ final Scriptable scope = ctx .newObject (globalScope );
130132 scope .setPrototype (globalScope );
131133 scope .setParentScope (null );
132134
133- for (Map .Entry <String , Object > entry : lookup .asMap ().entrySet ()) {
134- ScriptableObject .putProperty (scope , entry .getKey (), entry .getValue ());
135- }
135+ return new SearchScript () {
136136
137- if (vars != null ) {
138- for (Map .Entry <String , Object > entry : vars .entrySet ()) {
137+ @ Override
138+ public LeafSearchScript getLeafSearchScript (LeafReaderContext context ) throws IOException {
139+ final LeafSearchLookup leafLookup = lookup .getLeafSearchLookup (context );
140+ for (Map .Entry <String , Object > entry : leafLookup .asMap ().entrySet ()) {
139141 ScriptableObject .putProperty (scope , entry .getKey (), entry .getValue ());
140142 }
141- }
142143
143- return new JavaScriptSearchScript ((Script ) compiledScript , scope , lookup );
144+ if (vars != null ) {
145+ for (Map .Entry <String , Object > entry : vars .entrySet ()) {
146+ ScriptableObject .putProperty (scope , entry .getKey (), entry .getValue ());
147+ }
148+ }
149+
150+ return new JavaScriptSearchScript ((Script ) compiledScript , scope , leafLookup );
151+ }
152+ };
144153 } finally {
145154 Context .exit ();
146155 }
@@ -208,15 +217,15 @@ public Object unwrap(Object value) {
208217 }
209218 }
210219
211- public static class JavaScriptSearchScript implements SearchScript {
220+ public static class JavaScriptSearchScript implements LeafSearchScript {
212221
213222 private final Script script ;
214223
215224 private final Scriptable scope ;
216225
217- private final SearchLookup lookup ;
226+ private final LeafSearchLookup lookup ;
218227
219- public JavaScriptSearchScript (Script script , Scriptable scope , SearchLookup lookup ) {
228+ public JavaScriptSearchScript (Script script , Scriptable scope , LeafSearchLookup lookup ) {
220229 this .script = script ;
221230 this .scope = scope ;
222231 this .lookup = lookup ;
@@ -225,18 +234,16 @@ public JavaScriptSearchScript(Script script, Scriptable scope, SearchLookup look
225234 @ Override
226235 public void setScorer (Scorer scorer ) {
227236 Context ctx = Context .enter ();
228- ScriptableObject .putProperty (scope , "_score" , wrapFactory .wrapAsJavaObject (ctx , scope , new ScoreAccessor (scorer ), ScoreAccessor .class ));
229- Context .exit ();
230- }
231-
232- @ Override
233- public void setNextReader (LeafReaderContext context ) {
234- lookup .setNextReader (context );
237+ try {
238+ ScriptableObject .putProperty (scope , "_score" , wrapFactory .wrapAsJavaObject (ctx , scope , new ScoreAccessor (scorer ), ScoreAccessor .class ));
239+ } finally {
240+ Context .exit ();
241+ }
235242 }
236243
237244 @ Override
238- public void setNextDocId (int doc ) {
239- lookup .setNextDocId (doc );
245+ public void setDocument (int doc ) {
246+ lookup .setDocument (doc );
240247 }
241248
242249 @ Override
@@ -245,8 +252,8 @@ public void setNextVar(String name, Object value) {
245252 }
246253
247254 @ Override
248- public void setNextSource (Map <String , Object > source ) {
249- lookup .source ().setNextSource (source );
255+ public void setSource (Map <String , Object > source ) {
256+ lookup .source ().setSource (source );
250257 }
251258
252259 @ Override
0 commit comments