Apache Lucene open-source search software
LUCENE-8655: Add .getSource() method to FunctionScoreQuery
Alan Woodward1b077cf0e310
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 9c2d63420f8..04042ca7c51 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -205,6 +205,9 @@ New Features* LUCENE-8646: New interval functions: Intervals.prefix() and Intervals.wildcard()(Alan Woodward)+* LUCENE-8655: Add a getter in FunctionScoreQuery class in order to access to the+ underlying DoubleValuesSource. (Gérald Quaire via Alan Woodward)+Improvements* LUCENE-7997: Add BaseSimilarityTestCase to sanity check similarities.diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.javaindex 38b97329e73..5268a430a3a 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java@@ -64,6 +64,13 @@ public final class FunctionScoreQuery extends Query {return in;}+ /**+ * @return the underlying value source+ */+ public DoubleValuesSource getSource() {+ return source;+ }+/*** Returns a FunctionScoreQuery where the scores of a wrapped query are multiplied by* the value of a DoubleValuesSource.diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.javaindex 3d3f54e4931..952e6a4738b 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java@@ -220,4 +220,28 @@ public class TestFunctionScoreQuery extends FunctionTestSetup {reader.close();dir.close();}++ // check access to the score source of a functionScoreQuery+ public void testAccessToValueSource() throws Exception {++ FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "a")), DoubleValuesSource.constant(31));+ Query q2 = new FunctionScoreQuery(q1.getWrappedQuery(), q1.getSource());+ QueryUtils.check(q2);+ QueryUtils.checkEqual(q2, q1);++ FunctionScoreQuery q3 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "first")),+ DoubleValuesSource.fromIntField(INT_FIELD));+ Query q4 = new FunctionScoreQuery(q3.getWrappedQuery(), q3.getSource());+ QueryUtils.checkEqual(q3, q4);++ SimpleBindings bindings = new SimpleBindings();+ bindings.add("score", DoubleValuesSource.SCORES);+ Expression expr = JavascriptCompiler.compile("ln(score + 4)");+ FunctionScoreQuery q5 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "text")), expr.getDoubleValuesSource(bindings));+ Query q6 = new FunctionScoreQuery(q5.getWrappedQuery(), q5.getSource());+ QueryUtils.checkEqual(q5, q6);+++ }+}