Skip to content

Commit

Permalink
Remove setNextScore in SearchScript
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brwe authored and dadoonet committed Oct 7, 2014
1 parent f7b4a4d commit cd7756c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScoreAccessor;
import org.elasticsearch.script.ScriptEngineService;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.lookup.SearchLookup;
Expand Down Expand Up @@ -164,7 +165,7 @@ public PythonSearchScript(PyCode code, Map<String, Object> vars, SearchLookup lo

@Override
public void setScorer(Scorer scorer) {
lookup.setScorer(scorer);
pyVars.__setitem__("_score", Py.java2py(new ScoreAccessor(scorer)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.plugins.PluginsService;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -40,7 +43,10 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.scriptFilter;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -193,7 +199,7 @@ public void testCustomScriptBoost() throws Exception {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value * _score").lang("python"))))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value * _score.doubleValue()").lang("python"))))
).actionGet();

assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
Expand All @@ -208,7 +214,7 @@ public void testCustomScriptBoost() throws Exception {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("param1 * param2 * _score").param("param1", 2).param("param2", 2).lang("python"))))
.add(ScoreFunctionBuilders.scriptFunction("param1 * param2 * _score.doubleValue()").param("param1", 2).param("param2", 2).lang("python"))))
).actionGet();

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

assertThat((String) value, CoreMatchers.equalTo("bar"));
}

@Test
public void testScriptScoresNested() throws IOException {
createIndex("index");
ensureYellow();
index("index", "testtype", "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
refresh();
SearchResponse response = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(
functionScoreQuery(
functionScoreQuery().add(scriptFunction("1").lang("python")))
.add(scriptFunction("_score.doubleValue()").lang("python")))
.add(scriptFunction("_score.doubleValue()").lang("python")
)
)
)
).actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).score(), equalTo(1.0f));
}

@Test
public void testScriptScoresWithAgg() throws IOException {
createIndex("index");
ensureYellow();
index("index", "testtype", "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
refresh();
SearchResponse response = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery()
.add(scriptFunction("_score.doubleValue()").lang("python")
)
).aggregation(terms("score_agg").script("_score.doubleValue()").lang("python"))
)
).actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).score(), equalTo(1.0f));
assertThat(((Terms) response.getAggregations().asMap().get("score_agg")).getBuckets().get(0).getKeyAsNumber().floatValue(), Matchers.is(1f));
assertThat(((Terms) response.getAggregations().asMap().get("score_agg")).getBuckets().get(0).getDocCount(), Matchers.is(1l));
}
}

0 comments on commit cd7756c

Please sign in to comment.