26
26
import org .elasticsearch .index .query .functionscore .ScoreFunctionBuilders ;
27
27
import org .elasticsearch .plugins .PluginsService ;
28
28
import org .elasticsearch .script .ScriptService ;
29
+ import org .elasticsearch .search .aggregations .bucket .terms .Terms ;
29
30
import org .elasticsearch .search .sort .SortOrder ;
30
31
import org .elasticsearch .test .ElasticsearchIntegrationTest ;
31
32
import org .hamcrest .CoreMatchers ;
33
+ import org .hamcrest .Matchers ;
32
34
import org .junit .After ;
33
35
import org .junit .Test ;
34
36
37
+ import java .io .IOException ;
35
38
import java .util .Arrays ;
36
39
import java .util .List ;
37
40
import java .util .Map ;
40
43
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
41
44
import static org .elasticsearch .index .query .FilterBuilders .scriptFilter ;
42
45
import static org .elasticsearch .index .query .QueryBuilders .*;
46
+ import static org .elasticsearch .index .query .functionscore .ScoreFunctionBuilders .scriptFunction ;
47
+ import static org .elasticsearch .search .aggregations .AggregationBuilders .terms ;
43
48
import static org .elasticsearch .search .builder .SearchSourceBuilder .searchSource ;
49
+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertSearchResponse ;
44
50
import static org .hamcrest .CoreMatchers .is ;
45
51
import static org .hamcrest .Matchers .equalTo ;
46
52
@@ -193,7 +199,7 @@ public void testCustomScriptBoost() throws Exception {
193
199
response = client ().search (searchRequest ()
194
200
.searchType (SearchType .QUERY_THEN_FETCH )
195
201
.source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
196
- .add (ScoreFunctionBuilders .scriptFunction ("doc['num1'].value * _score" ).lang ("python" ))))
202
+ .add (ScoreFunctionBuilders .scriptFunction ("doc['num1'].value * _score.doubleValue() " ).lang ("python" ))))
197
203
).actionGet ();
198
204
199
205
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -208,7 +214,7 @@ public void testCustomScriptBoost() throws Exception {
208
214
response = client ().search (searchRequest ()
209
215
.searchType (SearchType .QUERY_THEN_FETCH )
210
216
.source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
211
- .add (ScoreFunctionBuilders .scriptFunction ("param1 * param2 * _score" ).param ("param1" , 2 ).param ("param2" , 2 ).lang ("python" ))))
217
+ .add (ScoreFunctionBuilders .scriptFunction ("param1 * param2 * _score.doubleValue() " ).param ("param1" , 2 ).param ("param2" , 2 ).lang ("python" ))))
212
218
).actionGet ();
213
219
214
220
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -239,4 +245,47 @@ public void testPythonEmptyParameters() throws Exception {
239
245
240
246
assertThat ((String ) value , CoreMatchers .equalTo ("bar" ));
241
247
}
248
+
249
+ @ Test
250
+ public void testScriptScoresNested () throws IOException {
251
+ createIndex ("index" );
252
+ ensureYellow ();
253
+ index ("index" , "testtype" , "1" , jsonBuilder ().startObject ().field ("dummy_field" , 1 ).endObject ());
254
+ refresh ();
255
+ SearchResponse response = client ().search (
256
+ searchRequest ().source (
257
+ searchSource ().query (
258
+ functionScoreQuery (
259
+ functionScoreQuery (
260
+ functionScoreQuery ().add (scriptFunction ("1" ).lang ("python" )))
261
+ .add (scriptFunction ("_score.doubleValue()" ).lang ("python" )))
262
+ .add (scriptFunction ("_score.doubleValue()" ).lang ("python" )
263
+ )
264
+ )
265
+ )
266
+ ).actionGet ();
267
+ assertSearchResponse (response );
268
+ assertThat (response .getHits ().getAt (0 ).score (), equalTo (1.0f ));
269
+ }
270
+
271
+ @ Test
272
+ public void testScriptScoresWithAgg () throws IOException {
273
+ createIndex ("index" );
274
+ ensureYellow ();
275
+ index ("index" , "testtype" , "1" , jsonBuilder ().startObject ().field ("dummy_field" , 1 ).endObject ());
276
+ refresh ();
277
+ SearchResponse response = client ().search (
278
+ searchRequest ().source (
279
+ searchSource ().query (
280
+ functionScoreQuery ()
281
+ .add (scriptFunction ("_score.doubleValue()" ).lang ("python" )
282
+ )
283
+ ).aggregation (terms ("score_agg" ).script ("_score.doubleValue()" ).lang ("python" ))
284
+ )
285
+ ).actionGet ();
286
+ assertSearchResponse (response );
287
+ assertThat (response .getHits ().getAt (0 ).score (), equalTo (1.0f ));
288
+ assertThat (((Terms ) response .getAggregations ().asMap ().get ("score_agg" )).getBuckets ().get (0 ).getKeyAsNumber ().floatValue (), Matchers .is (1f ));
289
+ assertThat (((Terms ) response .getAggregations ().asMap ().get ("score_agg" )).getBuckets ().get (0 ).getDocCount (), Matchers .is (1l ));
290
+ }
242
291
}
0 commit comments