diff --git a/src/main/java/org/elasticsearch/script/AbstractSearchScript.java b/src/main/java/org/elasticsearch/script/AbstractSearchScript.java index 3246495ae89c8..662531141fa49 100644 --- a/src/main/java/org/elasticsearch/script/AbstractSearchScript.java +++ b/src/main/java/org/elasticsearch/script/AbstractSearchScript.java @@ -95,7 +95,7 @@ void setLookup(SearchLookup lookup) { @Override public void setScorer(Scorer scorer) { - lookup.setScorer(scorer); + throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/org/elasticsearch/script/ScoreAccessor.java b/src/main/java/org/elasticsearch/script/ScoreAccessor.java index 38f83f1ac2734..93536e5c29e4d 100644 --- a/src/main/java/org/elasticsearch/script/ScoreAccessor.java +++ b/src/main/java/org/elasticsearch/script/ScoreAccessor.java @@ -19,6 +19,7 @@ package org.elasticsearch.script; +import org.apache.lucene.search.Scorer; import org.elasticsearch.search.lookup.DocLookup; import java.io.IOException; @@ -31,15 +32,15 @@ */ public final class ScoreAccessor extends Number { - final DocLookup doc; + Scorer scorer; - public ScoreAccessor(DocLookup d) { - doc = d; + public ScoreAccessor(Scorer scorer) { + this.scorer = scorer; } float score() { try { - return doc.score(); + return scorer.score(); } catch (IOException e) { throw new RuntimeException("Could not get score", e); } diff --git a/src/main/java/org/elasticsearch/script/ScriptService.java b/src/main/java/org/elasticsearch/script/ScriptService.java index 6afd1e19d0c92..b35b85e203357 100644 --- a/src/main/java/org/elasticsearch/script/ScriptService.java +++ b/src/main/java/org/elasticsearch/script/ScriptService.java @@ -230,9 +230,6 @@ public ScriptService(Settings settings, Environment env, Set params) { - return new DocScoreSearchScript(); - } - } - - public static class DocScoreSearchScript extends AbstractFloatSearchScript { - @Override - public float runAsFloat() { - try { - return doc().score(); - } catch (IOException e) { - return 0; - } - } - } } diff --git a/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java b/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java index f3e4118e6d59f..299d90e71a9c6 100644 --- a/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java +++ b/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java @@ -43,6 +43,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.script.*; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.search.suggest.term.TermSuggestion; import java.io.IOException; import java.math.BigDecimal; @@ -186,6 +187,7 @@ public static final class GroovyScript implements ExecutableScript, SearchScript private final SearchLookup lookup; private final Map variables; private final ESLogger logger; + private Scorer scorer; public GroovyScript(Script script, ESLogger logger) { this(script, null, logger); @@ -196,17 +198,12 @@ public GroovyScript(Script script, @Nullable SearchLookup lookup, ESLogger logge this.lookup = lookup; this.logger = logger; this.variables = script.getBinding().getVariables(); - if (lookup != null) { - // Add the _score variable, which will access score from lookup.doc() - this.variables.put("_score", new ScoreAccessor(lookup.doc())); - } } @Override public void setScorer(Scorer scorer) { - if (lookup != null) { - lookup.setScorer(scorer); - } + this.scorer = scorer; + this.variables.put("_score", new ScoreAccessor(scorer)); } @Override diff --git a/src/main/java/org/elasticsearch/search/lookup/DocLookup.java b/src/main/java/org/elasticsearch/search/lookup/DocLookup.java index 9cf56e66da9b8..78d8fb0180354 100644 --- a/src/main/java/org/elasticsearch/search/lookup/DocLookup.java +++ b/src/main/java/org/elasticsearch/search/lookup/DocLookup.java @@ -49,8 +49,6 @@ public class DocLookup implements Map { private AtomicReaderContext reader; - private Scorer scorer; - private int docId = -1; DocLookup(MapperService mapperService, IndexFieldDataService fieldDataService, @Nullable String[] types) { @@ -76,22 +74,10 @@ public void setNextReader(AtomicReaderContext context) { localCacheFieldData.clear(); } - public void setScorer(Scorer scorer) { - this.scorer = scorer; - } - public void setNextDocId(int docId) { this.docId = docId; } - public float score() throws IOException { - return scorer.score(); - } - - public float getScore() throws IOException { - return scorer.score(); - } - @Override public Object get(Object key) { // assume its a string... diff --git a/src/main/java/org/elasticsearch/search/lookup/SearchLookup.java b/src/main/java/org/elasticsearch/search/lookup/SearchLookup.java index 781e24a1c32e6..2dbda01a0e883 100644 --- a/src/main/java/org/elasticsearch/search/lookup/SearchLookup.java +++ b/src/main/java/org/elasticsearch/search/lookup/SearchLookup.java @@ -76,10 +76,6 @@ public DocLookup doc() { return this.docMap; } - public void setScorer(Scorer scorer) { - docMap.setScorer(scorer); - } - public void setNextReader(AtomicReaderContext context) { docMap.setNextReader(context); sourceLookup.setNextReader(context); diff --git a/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java b/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java index 423d741647dd6..bf327675ffbcc 100644 --- a/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java +++ b/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java @@ -1140,7 +1140,7 @@ public void script_Score() { .setQuery(functionScoreQuery(matchAllQuery()).add(ScoreFunctionBuilders.scriptFunction("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value"))) .addAggregation(terms("terms") .collectMode(randomFrom(SubAggCollectionMode.values())) - .script("ceil(_doc.score()/3)") + .script("ceil(_score.doubleValue()/3)") ).execute().actionGet(); assertSearchResponse(response); diff --git a/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java b/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java index edc438216f94a..0dcec77f83cd3 100644 --- a/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java +++ b/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java @@ -270,7 +270,7 @@ public void testFieldCollapsing() throws Exception { topHits("hits").setSize(1) ) .subAggregation( - max("max_score").script("_doc.score()") + max("max_score").script("_score.doubleValue()") ) ) .get(); diff --git a/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java b/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java index 47c1e06b9eb59..8f8646d5f0a9a 100644 --- a/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java +++ b/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; import org.elasticsearch.index.query.functionscore.weight.WeightBuilder; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; @@ -40,6 +41,7 @@ import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*; +import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; @@ -388,4 +390,44 @@ public void checkWeightOnlyCreatesBoostFunction() throws IOException { assertSearchResponse(response); assertThat(response.getHits().getAt(0).score(), equalTo(2.0f)); } + + @Test + public void testScriptScoresNested() throws IOException { + index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject()); + refresh(); + SearchResponse response = client().search( + searchRequest().source( + searchSource().query( + functionScoreQuery( + functionScoreQuery( + functionScoreQuery().add(scriptFunction("1"))) + .add(scriptFunction("_score.doubleValue()"))) + .add(scriptFunction("_score.doubleValue()") + ) + ) + ) + ).actionGet(); + assertSearchResponse(response); + assertThat(response.getHits().getAt(0).score(), equalTo(1.0f)); + } + + @Test + public void testScriptScoresWithAgg() throws IOException { + index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject()); + refresh(); + SearchResponse response = client().search( + searchRequest().source( + searchSource().query( + functionScoreQuery() + .add(scriptFunction("_score.doubleValue()") + ) + ).aggregation(terms("score_agg").script("_score.doubleValue()")) + ) + ).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(), is(1f)); + assertThat(((Terms) response.getAggregations().asMap().get("score_agg")).getBuckets().get(0).getDocCount(), is(1l)); + } } +