You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by tf...@apache.org on 2020/06/23 00:11:24 UTC
[lucene-solr] branch branch_8x updated: SOLR-14554: Let Solr use
WAND algorithm when scores are requested (#1566)
This is an automated email from the ASF dual-hosted git repository.
tflobbe pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git
The following commit(s) were added to refs/heads/branch_8x by this push:
new 885da40 SOLR-14554: Let Solr use WAND algorithm when scores are requested (#1566)
885da40 is described below
commit 885da40f34095bed9efa419f9b2774d3549d88df
Author: Tomas Fernandez Lobbe <tf...@apache.org>
AuthorDate: Mon Jun 22 16:50:54 2020 -0700
SOLR-14554: Let Solr use WAND algorithm when scores are requested (#1566)
Make MaxScoreCollector set minCompetitiveScore
---
solr/CHANGES.txt | 2 +
.../org/apache/solr/search/MaxScoreCollector.java | 6 +-
.../apache/solr/search/MaxScoreCollectorTest.java | 91 ++++++++++++++++++++++
.../apache/solr/search/SolrIndexSearcherTest.java | 8 +-
4 files changed, 102 insertions(+), 5 deletions(-)
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 476c67a..9a7d13d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -100,6 +100,8 @@ Optimizations
* SOLR-14552: Add BlockMax-WAND support to ReRank queries (Tomás Fernández Löbbe)
+* SOLR-14554: Add BlockMax-WAND support for queries where the score is requested (Tomás Fernández Löbbe)
+
Bug Fixes
---------------------
* SOLR-13264: IndexSizeTrigger aboveOp / belowOp properties not in valid properties.
diff --git a/solr/core/src/java/org/apache/solr/search/MaxScoreCollector.java b/solr/core/src/java/org/apache/solr/search/MaxScoreCollector.java
index 1b8987d..744e576 100644
--- a/solr/core/src/java/org/apache/solr/search/MaxScoreCollector.java
+++ b/solr/core/src/java/org/apache/solr/search/MaxScoreCollector.java
@@ -48,6 +48,10 @@ public class MaxScoreCollector extends SimpleCollector {
@Override
public void collect(int doc) throws IOException {
collectedAnyHits = true;
- maxScore = Math.max(scorer.score(), maxScore);
+ float docScore = scorer.score();
+ if (Float.compare(docScore, maxScore) > 0) {
+ maxScore = docScore;
+ scorer.setMinCompetitiveScore(Math.nextUp(maxScore));
+ }
}
}
diff --git a/solr/core/src/test/org/apache/solr/search/MaxScoreCollectorTest.java b/solr/core/src/test/org/apache/solr/search/MaxScoreCollectorTest.java
new file mode 100644
index 0000000..a5fb41e
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/search/MaxScoreCollectorTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.io.IOException;
+
+import org.apache.lucene.search.Scorable;
+import org.apache.solr.SolrTestCase;
+
+public class MaxScoreCollectorTest extends SolrTestCase {
+
+ public void test() throws IOException {
+ MaxScoreCollector collector = new MaxScoreCollector();
+ DummyScorer scorer = new DummyScorer();
+ collector.setScorer(scorer);
+ assertEquals(Float.NaN, collector.getMaxScore(), 0f);
+ assertEquals(0f, scorer.minCompetitiveScore, 0f);
+
+ collector.collect(0);
+ assertEquals(Float.MIN_VALUE, collector.getMaxScore(), 0f);
+ assertEquals(0f, scorer.minCompetitiveScore, 0f);
+
+ scorer.nextScore = 1f;
+ collector.collect(0);
+ assertEquals(1f, collector.getMaxScore(), 0f);
+ assertEquals(Math.nextUp(1f), scorer.minCompetitiveScore, 0f);
+
+ scorer.nextScore = 0f;
+ collector.collect(0);
+ assertEquals(1f, collector.getMaxScore(), 0f);
+ assertEquals(Math.nextUp(1f), scorer.minCompetitiveScore, 0f);
+
+ scorer.nextScore = -1f;
+ collector.collect(0);
+ assertEquals(1f, collector.getMaxScore(), 0f);
+ assertEquals(Math.nextUp(1f), scorer.minCompetitiveScore, 0f);
+
+ scorer.nextScore = Float.MAX_VALUE;
+ collector.collect(0);
+ assertEquals(Float.MAX_VALUE, collector.getMaxScore(), 0f);
+ assertEquals(Float.POSITIVE_INFINITY, scorer.minCompetitiveScore, 0f);
+
+ scorer.nextScore = Float.POSITIVE_INFINITY;
+ collector.collect(0);
+ assertEquals(Float.POSITIVE_INFINITY, collector.getMaxScore(), 0f);
+ assertEquals(Float.POSITIVE_INFINITY, scorer.minCompetitiveScore, 0f);
+
+
+ scorer.nextScore = Float.NaN;
+ collector.collect(0);
+ assertEquals(Float.NaN, collector.getMaxScore(), 0f);
+ assertEquals(Float.NaN, scorer.minCompetitiveScore, 0f);
+ }
+
+ private final static class DummyScorer extends Scorable {
+
+ float nextScore = 0f;
+ float minCompetitiveScore = 0f;
+
+ @Override
+ public float score() throws IOException {
+ return nextScore;
+ }
+
+ @Override
+ public int docID() {
+ return 0;
+ }
+
+ @Override
+ public void setMinCompetitiveScore(float minScore) throws IOException {
+ this.minCompetitiveScore = minScore;
+ }
+
+ }
+
+}
diff --git a/solr/core/src/test/org/apache/solr/search/SolrIndexSearcherTest.java b/solr/core/src/test/org/apache/solr/search/SolrIndexSearcherTest.java
index d1f557e..cb418e6 100644
--- a/solr/core/src/test/org/apache/solr/search/SolrIndexSearcherTest.java
+++ b/solr/core/src/test/org/apache/solr/search/SolrIndexSearcherTest.java
@@ -31,7 +31,7 @@ import org.junit.BeforeClass;
public class SolrIndexSearcherTest extends SolrTestCaseJ4 {
- private final static int NUM_DOCS = 20;
+ private final static int NUM_DOCS = 200;
@BeforeClass
public static void setUpClass() throws Exception {
@@ -42,8 +42,8 @@ public class SolrIndexSearcherTest extends SolrTestCaseJ4 {
"field2_s", String.valueOf(i % 2),
"field3_i_dvo", String.valueOf(i),
"field4_t", numbersTo(i)));
- assertU(commit()); //commit inside the loop to get multiple segments
}
+ assertU(commit());
}
private static String numbersTo(int i) {
@@ -199,7 +199,7 @@ public class SolrIndexSearcherTest extends SolrTestCaseJ4 {
h.getCore().withSearcher(searcher -> {
QueryCommand cmd = createBasicQueryCommand(1, 1, "field4_t", "0");
- Query filterQuery = new TermQuery(new Term("field4_t", "19"));
+ Query filterQuery = new TermQuery(new Term("field4_t", Integer.toString(NUM_DOCS - 1)));
cmd.setFilterList(filterQuery);
assertNull(searcher.getProcessedFilter(null, cmd.getFilterList()).postFilter);
assertMatchesEqual(1, searcher, cmd);
@@ -240,7 +240,7 @@ public class SolrIndexSearcherTest extends SolrTestCaseJ4 {
h.getCore().withSearcher(searcher -> {
QueryCommand cmd = createBasicQueryCommand(1, 1, "field4_t", "0");
// Use ScoreMode.COMPLETE for the PostFilter
- MockPostFilter filterQuery = new MockPostFilter(100, 101, ScoreMode.COMPLETE);
+ MockPostFilter filterQuery = new MockPostFilter(NUM_DOCS * 10, 101, ScoreMode.COMPLETE);
cmd.setFilterList(filterQuery);
assertNotNull(searcher.getProcessedFilter(null, cmd.getFilterList()).postFilter);
assertMatchesEqual(NUM_DOCS, searcher, cmd);