21
21
22
22
import org .elasticsearch .action .search .SearchResponse ;
23
23
import org .elasticsearch .action .search .SearchType ;
24
+ import org .elasticsearch .index .query .functionscore .ScoreFunctionBuilders ;
24
25
import org .elasticsearch .search .sort .SortOrder ;
25
26
import org .elasticsearch .test .ElasticsearchIntegrationTest ;
26
27
import org .junit .Test ;
@@ -112,11 +113,7 @@ public void testScriptFieldUsingSource() throws Exception {
112
113
.addScriptField ("s_obj2_arr2" , "js" , "_source.obj2.arr2" , null )
113
114
.execute ().actionGet ();
114
115
115
- Map <String , Object > sObj1 = (Map <String , Object >) response .getHits ().getAt (0 ).field ("_source.obj1" ).value ();
116
- assertThat (sObj1 .get ("test" ).toString (), equalTo ("something" ));
117
- assertThat (response .getHits ().getAt (0 ).field ("s_obj1_test" ).value ().toString (), equalTo ("something" ));
118
-
119
- sObj1 = (Map <String , Object >) response .getHits ().getAt (0 ).field ("s_obj1" ).value ();
116
+ Map <String , Object > sObj1 = (Map <String , Object >) response .getHits ().getAt (0 ).field ("s_obj1" ).value ();
120
117
assertThat (sObj1 .get ("test" ).toString (), equalTo ("something" ));
121
118
assertThat (response .getHits ().getAt (0 ).field ("s_obj1_test" ).value ().toString (), equalTo ("something" ));
122
119
@@ -145,7 +142,8 @@ public void testCustomScriptBoost() throws Exception {
145
142
logger .info (" --> running doc['num1'].value" );
146
143
SearchResponse response = client ().search (searchRequest ()
147
144
.searchType (SearchType .QUERY_THEN_FETCH )
148
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("doc['num1'].value" ).lang ("js" )))
145
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
146
+ .add (ScoreFunctionBuilders .scriptFunction ("doc['num1'].value" ).lang ("js" ))))
149
147
).actionGet ();
150
148
151
149
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -159,7 +157,8 @@ public void testCustomScriptBoost() throws Exception {
159
157
logger .info (" --> running -doc['num1'].value" );
160
158
response = client ().search (searchRequest ()
161
159
.searchType (SearchType .QUERY_THEN_FETCH )
162
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("-doc['num1'].value" ).lang ("js" )))
160
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
161
+ .add (ScoreFunctionBuilders .scriptFunction ("-doc['num1'].value" ).lang ("js" ))))
163
162
).actionGet ();
164
163
165
164
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -174,7 +173,8 @@ public void testCustomScriptBoost() throws Exception {
174
173
logger .info (" --> running pow(doc['num1'].value, 2)" );
175
174
response = client ().search (searchRequest ()
176
175
.searchType (SearchType .QUERY_THEN_FETCH )
177
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("Math.pow(doc['num1'].value, 2)" ).lang ("js" )))
176
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
177
+ .add (ScoreFunctionBuilders .scriptFunction ("Math.pow(doc['num1'].value, 2)" ).lang ("js" ))))
178
178
).actionGet ();
179
179
180
180
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -188,7 +188,8 @@ public void testCustomScriptBoost() throws Exception {
188
188
logger .info (" --> running max(doc['num1'].value, 1)" );
189
189
response = client ().search (searchRequest ()
190
190
.searchType (SearchType .QUERY_THEN_FETCH )
191
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("Math.max(doc['num1'].value, 1)" ).lang ("js" )))
191
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
192
+ .add (ScoreFunctionBuilders .scriptFunction ("Math.max(doc['num1'].value, 1)" ).lang ("js" ))))
192
193
).actionGet ();
193
194
194
195
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -202,7 +203,8 @@ public void testCustomScriptBoost() throws Exception {
202
203
logger .info (" --> running doc['num1'].value * _score" );
203
204
response = client ().search (searchRequest ()
204
205
.searchType (SearchType .QUERY_THEN_FETCH )
205
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("doc['num1'].value * _score" ).lang ("js" )))
206
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
207
+ .add (ScoreFunctionBuilders .scriptFunction ("doc['num1'].value * _score" ).lang ("js" ))))
206
208
).actionGet ();
207
209
208
210
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
@@ -216,7 +218,8 @@ public void testCustomScriptBoost() throws Exception {
216
218
logger .info (" --> running param1 * param2 * _score" );
217
219
response = client ().search (searchRequest ()
218
220
.searchType (SearchType .QUERY_THEN_FETCH )
219
- .source (searchSource ().explain (true ).query (customScoreQuery (termQuery ("test" , "value" )).script ("param1 * param2 * _score" ).param ("param1" , 2 ).param ("param2" , 2 ).lang ("js" )))
221
+ .source (searchSource ().explain (true ).query (functionScoreQuery (termQuery ("test" , "value" ))
222
+ .add (ScoreFunctionBuilders .scriptFunction ("param1 * param2 * _score" ).param ("param1" , 2 ).param ("param2" , 2 ).lang ("js" ))))
220
223
).actionGet ();
221
224
222
225
assertThat ("Failures " + Arrays .toString (response .getShardFailures ()), response .getShardFailures ().length , equalTo (0 ));
0 commit comments