cpoerschke commented on code in PR #1324:
URL: https://github.com/apache/solr/pull/1324#discussion_r1111680488


##########
solr/core/src/java/org/apache/solr/search/ReRankOperator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.search;
+
+import java.util.Locale;
+import org.apache.solr.common.SolrException;
+
+public enum ReRankOperator {
+  ADD,
+  MULTIPLY;
+
+  public static ReRankOperator get(String p) {
+    if (p != null) {
+      try {
+        return ReRankOperator.valueOf(p.toUpperCase(Locale.ROOT));
+      } catch (Exception ex) {
+        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid 
reRankOperator");

Review Comment:
   minor/subjective
   ```suggestion
           throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, 
"Invalid reRankOperator: " + p);
   ```



##########
solr/core/src/java/org/apache/solr/search/ReRankOperator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.search;
+
+import java.util.Locale;
+import org.apache.solr.common.SolrException;
+
+public enum ReRankOperator {
+  ADD,
+  MULTIPLY;
+
+  public static ReRankOperator get(String p) {
+    if (p != null) {
+      try {
+        return ReRankOperator.valueOf(p.toUpperCase(Locale.ROOT));
+      } catch (Exception ex) {
+        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid 
reRankOperator");
+      }
+    }
+    return null;
+  }
+
+  public boolean isEqual(String s) {
+    return s != null && toString().equals(s.toUpperCase(Locale.ROOT));
+  }
+

Review Comment:
   Wondering if this code is unused?
   ```suggestion
   ```



##########
solr/core/src/java/org/apache/solr/search/ReRankQParserPlugin.java:
##########
@@ -68,29 +71,48 @@ public Query parse() throws SyntaxError {
       Query reRankQuery = reRankParser.parse();
 
       int reRankDocs = localParams.getInt(RERANK_DOCS, RERANK_DOCS_DEFAULT);
-      reRankDocs = Math.max(1, reRankDocs); //
+      reRankDocs = Math.max(1, reRankDocs);
 
       double reRankWeight = localParams.getDouble(RERANK_WEIGHT, 
RERANK_WEIGHT_DEFAULT);
 
-      return new ReRankQuery(reRankQuery, reRankDocs, reRankWeight);
+      ReRankOperator reRankOperator =
+          ReRankOperator.get(localParams.get(RERANK_OPERATOR, 
RERANK_OPERATOR_DEFAULT));
+
+      return new ReRankQuery(reRankQuery, reRankDocs, reRankWeight, 
reRankOperator);
     }
   }
 
   private static final class ReRankQueryRescorer extends QueryRescorer {
 
-    final double reRankWeight;
+    final BiFloatFunction scoreCombiner;
+
+    @FunctionalInterface
+    interface BiFloatFunction {
+      float func(float a, float b);
+    }
 
-    public ReRankQueryRescorer(Query reRankQuery, double reRankWeight) {
+    public ReRankQueryRescorer(
+        Query reRankQuery, double reRankWeight, ReRankOperator reRankOperator) 
{
       super(reRankQuery);
-      this.reRankWeight = reRankWeight;
+      switch (reRankOperator) {
+        case ADD:
+          scoreCombiner = (score, second) -> (float) (score + reRankWeight * 
second);
+          break;
+        case MULTIPLY:
+          scoreCombiner = (score, second) -> (float) (score * reRankWeight * 
second);
+          break;
+        default:
+          scoreCombiner = null;
+          throw new IllegalArgumentException("Unexpected:" + reRankOperator);

Review Comment:
   ```suggestion
             throw new IllegalArgumentException("Unexpected: reRankOperator=" + 
reRankOperator);
   ```



##########
solr/core/src/test/org/apache/solr/search/TestReRankQParserPlugin.java:
##########
@@ -126,6 +128,58 @@ public void testReRankQueries() {
         "//result/doc[5]/str[@name='id'][.='1']",
         "//result/doc[6]/str[@name='id'][.='5']");
 
+    // test with reRankOperator=add by checking score (the order doesn't 
change)
+    params = new ModifiableSolrParams();
+    params.add(
+        "rq",
+        "{!"
+            + ReRankQParserPlugin.NAME
+            + " "
+            + ReRankQParserPlugin.RERANK_QUERY
+            + "=$rqq "
+            + ReRankQParserPlugin.RERANK_OPERATOR
+            + "=add "
+            + ReRankQParserPlugin.RERANK_DOCS
+            + "=200}");
+    params.add("q", "term_s:YYYY^=0.1"); // force score=0.1
+    params.add("rqq", "{!edismax bf=$bff}*:*");
+    params.add("bff", "field(test_ti)"); // test_ti=5000 for item 3
+    params.add("start", "0");
+    params.add("rows", "6");
+    params.add("df", "text");
+    params.add("fl", "id,score");
+    assertQ(
+        req(params),
+        "*[count(//doc)=6]",
+        "//result/doc[1]/str[@name='id'][.='3']",
+        "//result/doc[1]/float[@name='score'][.='10002.1']"); // multiplying 
gives 1000.2 instead
+
+    // test with reRankOperator=multiply by checking score (the order doesn't 
change)
+    params = new ModifiableSolrParams();
+    params.add(
+        "rq",
+        "{!"
+            + ReRankQParserPlugin.NAME
+            + " "
+            + ReRankQParserPlugin.RERANK_QUERY
+            + "=$rqq "
+            + ReRankQParserPlugin.RERANK_OPERATOR
+            + "=multiply "

Review Comment:
   Perhaps `mult` could be an additional test case i.e. to test error handling 
and that `mult-instead-of-multiply` or typos will explicitly fail and not be 
silently be treated as `add`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to