You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2019/07/08 08:28:18 UTC

[lucene-solr] branch branch_8x updated: LUCENE-8803: Change the way that reverse ordering is implemented.

This is an automated email from the ASF dual-hosted git repository.

jpountz 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 a329953  LUCENE-8803: Change the way that reverse ordering is implemented.
a329953 is described below

commit a329953429b6fa40ccf94a0253cf892b329edc3c
Author: Adrien Grand <jp...@gmail.com>
AuthorDate: Mon Jul 8 10:18:02 2019 +0200

    LUCENE-8803: Change the way that reverse ordering is implemented.
    
    This addresses some test failures when IndexSearcher is created with an executor
    and merges hits with TopDocs#merge.
---
 .../org/apache/lucene/document/FeatureField.java   |  2 +-
 .../apache/lucene/document/FeatureSortField.java   |  8 ++--
 .../apache/lucene/document/TestFeatureSort.java    | 50 ++++++++++++++++++++++
 3 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/document/FeatureField.java b/lucene/core/src/java/org/apache/lucene/document/FeatureField.java
index 7cf22ef..229e057 100644
--- a/lucene/core/src/java/org/apache/lucene/document/FeatureField.java
+++ b/lucene/core/src/java/org/apache/lucene/document/FeatureField.java
@@ -138,7 +138,7 @@ public final class FeatureField extends Field {
     }
     if (featureValue < Float.MIN_NORMAL) {
       throw new IllegalArgumentException("featureValue must be a positive normal float, got: " +
-          featureValue + "for feature " + fieldsData + " on field " + name +
+          featureValue + " for feature " + fieldsData + " on field " + name +
           " which is less than the minimum positive normal float: " + Float.MIN_NORMAL);
     }
     this.featureValue = featureValue;
diff --git a/lucene/core/src/java/org/apache/lucene/document/FeatureSortField.java b/lucene/core/src/java/org/apache/lucene/document/FeatureSortField.java
index 1a6df72..1e73f8c 100644
--- a/lucene/core/src/java/org/apache/lucene/document/FeatureSortField.java
+++ b/lucene/core/src/java/org/apache/lucene/document/FeatureSortField.java
@@ -42,7 +42,7 @@ final class FeatureSortField extends SortField {
    * @param featureName The name of the feature to use for the sort value
    */
   public FeatureSortField(String field, String featureName) {
-    super(Objects.requireNonNull(field), SortField.Type.CUSTOM);
+    super(Objects.requireNonNull(field), SortField.Type.CUSTOM, true);
     this.featureName = Objects.requireNonNull(featureName);
   }
   
@@ -128,12 +128,12 @@ final class FeatureSortField extends SortField {
 
     @Override
     public int compare(int slot1, int slot2) {
-      return Float.compare(values[slot2], values[slot1]);
+      return Float.compare(values[slot1], values[slot2]);
     }
 
     @Override
     public int compareBottom(int doc) throws IOException {
-      return Float.compare(getValueForDoc(doc), bottom);
+      return Float.compare(bottom, getValueForDoc(doc));
     }
 
     @Override
@@ -158,7 +158,7 @@ final class FeatureSortField extends SortField {
 
     @Override
     public int compareTop(int doc) throws IOException {
-      return Float.compare(getValueForDoc(doc), topValue);
+      return Float.compare(topValue, getValueForDoc(doc));
     }
   }
 }
diff --git a/lucene/core/src/test/org/apache/lucene/document/TestFeatureSort.java b/lucene/core/src/test/org/apache/lucene/document/TestFeatureSort.java
index 47e29c0..3c8fef1 100644
--- a/lucene/core/src/test/org/apache/lucene/document/TestFeatureSort.java
+++ b/lucene/core/src/test/org/apache/lucene/document/TestFeatureSort.java
@@ -21,12 +21,15 @@ import java.io.IOException;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.CheckHits;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.TestUtil;
 
 /*
  * Test for sorting using a feature from a FeatureField.
@@ -216,4 +219,51 @@ public class TestFeatureSort extends LuceneTestCase {
     ir.close();
     dir.close();
   }
+
+  // This duel gives compareBottom and compareTop some coverage
+  public void testDuelFloat() throws IOException {
+    Directory dir = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+    int numDocs = atLeast(100);
+    for (int d = 0; d < numDocs; ++d) {
+      Document doc = new Document();
+      if (random().nextBoolean()) {
+        float f;
+        do {
+          int freq = TestUtil.nextInt(random(), 1, (1 << 16) - 1);
+          f = FeatureField.decodeFeatureValue(freq);
+        } while (f < Float.MIN_NORMAL);
+        doc.add(new NumericDocValuesField("float", Float.floatToIntBits(f)));
+        doc.add(new FeatureField("feature", "foo", f));
+      }
+      w.addDocument(doc);
+    }
+
+    IndexReader r = w.getReader();
+    w.close();
+    IndexSearcher searcher = newSearcher(r);
+
+    TopDocs topDocs = null;
+    TopDocs featureTopDocs = null;
+    do {
+      if (topDocs == null) {
+        topDocs = searcher.search(new MatchAllDocsQuery(), 10,
+            new Sort(new SortField("float", SortField.Type.FLOAT, true)));
+        featureTopDocs = searcher.search(new MatchAllDocsQuery(), 10,
+            new Sort(FeatureField.newFeatureSort("feature", "foo")));
+      } else {
+        topDocs = searcher.searchAfter(topDocs.scoreDocs[topDocs.scoreDocs.length - 1],
+            new MatchAllDocsQuery(), 10,
+            new Sort(new SortField("float", SortField.Type.FLOAT, true)));
+        featureTopDocs = searcher.searchAfter(featureTopDocs.scoreDocs[featureTopDocs.scoreDocs.length - 1],
+            new MatchAllDocsQuery(), 10,
+            new Sort(FeatureField.newFeatureSort("feature", "foo")));
+      }
+
+      CheckHits.checkEqual(new MatchAllDocsQuery(), topDocs.scoreDocs, featureTopDocs.scoreDocs);
+    } while (topDocs.scoreDocs.length > 0);
+
+    r.close();
+    dir.close();
+  }
 }