Skip to content

Commit cd7756c

Browse files
brwedadoonet
authored andcommitted
Remove setNextScore in SearchScript
Due to a change in elasticsearch 1.4.0, we need to apply a similar patch here. See #6864 See #7819 Closes #16. Closes #21.
1 parent f7b4a4d commit cd7756c

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.inject.Inject;
2727
import org.elasticsearch.common.settings.Settings;
2828
import org.elasticsearch.script.ExecutableScript;
29+
import org.elasticsearch.script.ScoreAccessor;
2930
import org.elasticsearch.script.ScriptEngineService;
3031
import org.elasticsearch.script.SearchScript;
3132
import org.elasticsearch.search.lookup.SearchLookup;
@@ -164,7 +165,7 @@ public PythonSearchScript(PyCode code, Map<String, Object> vars, SearchLookup lo
164165

165166
@Override
166167
public void setScorer(Scorer scorer) {
167-
lookup.setScorer(scorer);
168+
pyVars.__setitem__("_score", Py.java2py(new ScoreAccessor(scorer)));
168169
}
169170

170171
@Override

src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
2727
import org.elasticsearch.plugins.PluginsService;
2828
import org.elasticsearch.script.ScriptService;
29+
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
2930
import org.elasticsearch.search.sort.SortOrder;
3031
import org.elasticsearch.test.ElasticsearchIntegrationTest;
3132
import org.hamcrest.CoreMatchers;
33+
import org.hamcrest.Matchers;
3234
import org.junit.After;
3335
import org.junit.Test;
3436

37+
import java.io.IOException;
3538
import java.util.Arrays;
3639
import java.util.List;
3740
import java.util.Map;
@@ -40,7 +43,10 @@
4043
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
4144
import static org.elasticsearch.index.query.FilterBuilders.scriptFilter;
4245
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;
4348
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
49+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
4450
import static org.hamcrest.CoreMatchers.is;
4551
import static org.hamcrest.Matchers.equalTo;
4652

@@ -193,7 +199,7 @@ public void testCustomScriptBoost() throws Exception {
193199
response = client().search(searchRequest()
194200
.searchType(SearchType.QUERY_THEN_FETCH)
195201
.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"))))
197203
).actionGet();
198204

199205
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@@ -208,7 +214,7 @@ public void testCustomScriptBoost() throws Exception {
208214
response = client().search(searchRequest()
209215
.searchType(SearchType.QUERY_THEN_FETCH)
210216
.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"))))
212218
).actionGet();
213219

214220
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@@ -239,4 +245,47 @@ public void testPythonEmptyParameters() throws Exception {
239245

240246
assertThat((String) value, CoreMatchers.equalTo("bar"));
241247
}
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+
}
242291
}

0 commit comments

Comments
 (0)