Scripting: Make _score in groovy scripts comparable
closes #8828 closes #9094
This commit is contained in:
parent
f83909f7ae
commit
6304f68715
|
@ -30,7 +30,7 @@ import java.io.IOException;
|
||||||
* The provided {@link DocLookup} is used to retrieve the score
|
* The provided {@link DocLookup} is used to retrieve the score
|
||||||
* for the current document.
|
* for the current document.
|
||||||
*/
|
*/
|
||||||
public final class ScoreAccessor extends Number {
|
public final class ScoreAccessor extends Number implements Comparable<Number> {
|
||||||
|
|
||||||
Scorer scorer;
|
Scorer scorer;
|
||||||
|
|
||||||
|
@ -65,4 +65,9 @@ public final class ScoreAccessor extends Number {
|
||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return score();
|
return score();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Number o) {
|
||||||
|
return Float.compare(this.score(), o.floatValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,20 +117,33 @@ public class GroovyScriptTests extends ElasticsearchIntegrationTest {
|
||||||
client().prepareIndex("test", "doc", "3").setSource("foo", "dog spiders that can eat a dog", "bar", 3).get();
|
client().prepareIndex("test", "doc", "3").setSource("foo", "dog spiders that can eat a dog", "bar", 3).get();
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
// _score access
|
|
||||||
SearchResponse resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("foo", "dog"))
|
|
||||||
.add(scriptFunction("_score", "groovy"))
|
|
||||||
.boostMode(CombineFunction.REPLACE)).get();
|
|
||||||
|
|
||||||
assertNoFailures(resp);
|
|
||||||
assertSearchHits(resp, "3", "1");
|
|
||||||
|
|
||||||
// doc[] access
|
// doc[] access
|
||||||
resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery())
|
SearchResponse resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery())
|
||||||
.add(scriptFunction("doc['bar'].value", "groovy"))
|
.add(scriptFunction("doc['bar'].value", "groovy"))
|
||||||
.boostMode(CombineFunction.REPLACE)).get();
|
.boostMode(CombineFunction.REPLACE)).get();
|
||||||
|
|
||||||
assertNoFailures(resp);
|
assertNoFailures(resp);
|
||||||
assertOrderedSearchHits(resp, "3", "2", "1");
|
assertOrderedSearchHits(resp, "3", "2", "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testScoreAccess() {
|
||||||
|
client().prepareIndex("test", "doc", "1").setSource("foo", "quick brow fox jumped over the lazy dog", "bar", 1).get();
|
||||||
|
client().prepareIndex("test", "doc", "2").setSource("foo", "fast jumping spiders", "bar", 2).get();
|
||||||
|
client().prepareIndex("test", "doc", "3").setSource("foo", "dog spiders that can eat a dog", "bar", 3).get();
|
||||||
|
refresh();
|
||||||
|
|
||||||
|
// _score can be accessed
|
||||||
|
SearchResponse resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("foo", "dog"))
|
||||||
|
.add(scriptFunction("_score", "groovy"))
|
||||||
|
.boostMode(CombineFunction.REPLACE)).get();
|
||||||
|
assertNoFailures(resp);
|
||||||
|
assertSearchHits(resp, "3", "1");
|
||||||
|
|
||||||
|
// _score is comparable
|
||||||
|
resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("foo", "dog"))
|
||||||
|
.add(scriptFunction("_score > 0 ? _score : 0", "groovy"))
|
||||||
|
.boostMode(CombineFunction.REPLACE)).get();
|
||||||
|
assertNoFailures(resp);
|
||||||
|
assertSearchHits(resp, "3", "1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue