You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2019/04/03 09:18:32 UTC

[lucene-solr] branch branch_8x updated: LUCENE-8750: Add setMissingValue to sorts from Double/LongValuesSource

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

romseygeek 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 6f47062  LUCENE-8750: Add setMissingValue to sorts from Double/LongValuesSource
6f47062 is described below

commit 6f47062a9b0a4d76562f15c4f1a3aec6acf27295
Author: Alan Woodward <ro...@apache.org>
AuthorDate: Wed Apr 3 09:42:36 2019 +0100

    LUCENE-8750: Add setMissingValue to sorts from Double/LongValuesSource
---
 lucene/CHANGES.txt                                 |  3 ++
 .../apache/lucene/search/DoubleValuesSource.java   | 25 ++++++++++++--
 .../org/apache/lucene/search/LongValuesSource.java | 26 +++++++++++++--
 .../lucene/search/TestDoubleValuesSource.java      | 38 +++++++++++++++++++--
 .../apache/lucene/search/TestLongValuesSource.java | 39 ++++++++++++++++++++--
 5 files changed, 120 insertions(+), 11 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 0a99deb..17ab566 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -70,6 +70,9 @@ Improvements
 * LUCENE-8732: ConstantScoreQuery can now early terminate the query if the minimum score is
   greater than the constant score and total hits are not requested. (Jim Ferenczi)
 
+* LUCENE-8750: Implements setMissingValue() on sort fields produced from 
+  DoubleValuesSource and LongValuesSource (Mike Sokolov via Alan Woodward)
+
 Changes in Runtime Behavior
 
 * LUCENE-8671: Load FST off-heap also for ID-like fields if reader is not opened
diff --git a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
index 6446f7f..bb3ef80 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
@@ -429,6 +429,16 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
     }
 
     @Override
+    public void setMissingValue(Object missingValue) {
+      if (missingValue instanceof Number) {
+        this.missingValue = missingValue;
+        ((DoubleValuesComparatorSource) getComparatorSource()).setMissingValue(((Number) missingValue).doubleValue());
+      } else {
+        super.setMissingValue(missingValue);
+      }
+    }
+
+    @Override
     public boolean needsScores() {
       return producer.needsScores();
     }
@@ -444,8 +454,13 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
 
     @Override
     public SortField rewrite(IndexSearcher searcher) throws IOException {
-      return new DoubleValuesSortField(producer.rewrite(searcher), reverse);
+      DoubleValuesSortField rewritten = new DoubleValuesSortField(producer.rewrite(searcher), reverse);
+      if (missingValue != null) {
+        rewritten.setMissingValue(missingValue);
+      }
+      return rewritten;
     }
+
   }
 
   private static class DoubleValuesHolder {
@@ -454,15 +469,21 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
 
   private static class DoubleValuesComparatorSource extends FieldComparatorSource {
     private final DoubleValuesSource producer;
+    private double missingValue;
 
     DoubleValuesComparatorSource(DoubleValuesSource producer) {
       this.producer = producer;
+      this.missingValue = 0d;
+    }
+
+    void setMissingValue(double missingValue) {
+      this.missingValue = missingValue;
     }
 
     @Override
     public FieldComparator<Double> newComparator(String fieldname, int numHits,
                                                int sortPos, boolean reversed) {
-      return new FieldComparator.DoubleComparator(numHits, fieldname, 0.0){
+      return new FieldComparator.DoubleComparator(numHits, fieldname, missingValue){
 
         LeafReaderContext ctx;
         DoubleValuesHolder holder = new DoubleValuesHolder();
diff --git a/lucene/core/src/java/org/apache/lucene/search/LongValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/LongValuesSource.java
index 867d411..1eafc22 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LongValuesSource.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LongValuesSource.java
@@ -280,6 +280,16 @@ public abstract class LongValuesSource implements SegmentCacheable {
     }
 
     @Override
+    public void setMissingValue(Object missingValue) {
+      if (missingValue instanceof Number) {
+        this.missingValue = missingValue;
+        ((LongValuesComparatorSource) getComparatorSource()).setMissingValue(((Number) missingValue).longValue());
+      } else {
+          super.setMissingValue(missingValue);
+      }
+    }
+
+    @Override
     public boolean needsScores() {
       return producer.needsScores();
     }
@@ -295,7 +305,11 @@ public abstract class LongValuesSource implements SegmentCacheable {
 
     @Override
     public SortField rewrite(IndexSearcher searcher) throws IOException {
-      return new LongValuesSortField(producer.rewrite(searcher), reverse);
+      LongValuesSortField rewritten = new LongValuesSortField(producer.rewrite(searcher), reverse);
+      if (missingValue != null) {
+        rewritten.setMissingValue(missingValue);
+      }
+      return rewritten;
     }
   }
 
@@ -305,15 +319,21 @@ public abstract class LongValuesSource implements SegmentCacheable {
 
   private static class LongValuesComparatorSource extends FieldComparatorSource {
     private final LongValuesSource producer;
+    private long missingValue;
 
     public LongValuesComparatorSource(LongValuesSource producer) {
       this.producer = producer;
+      this.missingValue = 0L;
+    }
+
+    void setMissingValue(long missingValue) {
+      this.missingValue = missingValue;
     }
 
     @Override
     public FieldComparator<Long> newComparator(String fieldname, int numHits,
-                                                 int sortPos, boolean reversed) {
-      return new FieldComparator.LongComparator(numHits, fieldname, 0L){
+                                               int sortPos, boolean reversed) {
+      return new FieldComparator.LongComparator(numHits, fieldname, missingValue) {
 
         LeafReaderContext ctx;
         LongValuesHolder holder = new LongValuesHolder();
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java b/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java
index a6cd8e1..73e34c2 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java
@@ -38,6 +38,8 @@ import org.apache.lucene.util.TestUtil;
 
 public class TestDoubleValuesSource extends LuceneTestCase {
 
+  private static final double LEAST_DOUBLE_VALUE = 45.72;
+
   private Directory dir;
   private IndexReader reader;
   private IndexSearcher searcher;
@@ -57,7 +59,7 @@ public class TestDoubleValuesSource extends LuceneTestCase {
       document.add(new FloatDocValuesField("float", random().nextFloat()));
       document.add(new DoubleDocValuesField("double", random().nextDouble()));
       if (i == 545)
-        document.add(new DoubleDocValuesField("onefield", 45.72));
+        document.add(new DoubleDocValuesField("onefield", LEAST_DOUBLE_VALUE));
       iw.addDocument(document);
     }
     reader = iw.getReader();
@@ -72,11 +74,41 @@ public class TestDoubleValuesSource extends LuceneTestCase {
     super.tearDown();
   }
 
-  public void testSortMissing() throws Exception {
+  public void testSortMissingZeroDefault() throws Exception {
+    // docs w/no value get default missing value = 0
+
     DoubleValuesSource onefield = DoubleValuesSource.fromDoubleField("onefield");
+    // sort decreasing
     TopDocs results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(onefield.getSortField(true)));
     FieldDoc first = (FieldDoc) results.scoreDocs[0];
-    assertEquals(45.72, first.fields[0]);
+    assertEquals(LEAST_DOUBLE_VALUE, first.fields[0]);
+
+    // sort increasing
+    results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(onefield.getSortField(false)));
+    first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(0d, first.fields[0]);
+  }
+
+  public void testSortMissingExplicit() throws Exception {
+    // docs w/no value get provided missing value
+
+    DoubleValuesSource onefield = DoubleValuesSource.fromDoubleField("onefield");
+
+    // sort decreasing, missing last
+    SortField oneFieldSort = onefield.getSortField(true);
+    oneFieldSort.setMissingValue(Double.MIN_VALUE);
+
+    TopDocs results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(oneFieldSort));
+    FieldDoc first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(LEAST_DOUBLE_VALUE, first.fields[0]);
+
+    // sort increasing, missing last
+    oneFieldSort = onefield.getSortField(false);
+    oneFieldSort.setMissingValue(Double.MAX_VALUE);
+
+    results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(oneFieldSort));
+    first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(LEAST_DOUBLE_VALUE, first.fields[0]);
   }
 
   public void testSimpleFieldEquivalences() throws Exception {
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java b/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java
index dd6694e..6380b73 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java
@@ -34,6 +34,8 @@ import org.apache.lucene.util.TestUtil;
 
 public class TestLongValuesSource extends LuceneTestCase {
 
+  private static final long LEAST_LONG_VALUE = 45L;
+
   private Directory dir;
   private IndexReader reader;
   private IndexSearcher searcher;
@@ -44,6 +46,7 @@ public class TestLongValuesSource extends LuceneTestCase {
     dir = newDirectory();
     RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
     int numDocs = TestUtil.nextInt(random(), 2049, 4000);
+    int leastValue = 45;
     for (int i = 0; i < numDocs; i++) {
       Document document = new Document();
       document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO));
@@ -51,7 +54,7 @@ public class TestLongValuesSource extends LuceneTestCase {
       document.add(new NumericDocValuesField("int", random().nextInt()));
       document.add(new NumericDocValuesField("long", random().nextLong()));
       if (i == 545)
-        document.add(new NumericDocValuesField("onefield", 45));
+        document.add(new NumericDocValuesField("onefield", LEAST_LONG_VALUE));
       iw.addDocument(document);
     }
     reader = iw.getReader();
@@ -66,11 +69,41 @@ public class TestLongValuesSource extends LuceneTestCase {
     super.tearDown();
   }
 
-  public void testSortMissing() throws Exception {
+  public void testSortMissingZeroDefault() throws Exception {
+    // docs w/no value get default missing value = 0
+
     LongValuesSource onefield = LongValuesSource.fromLongField("onefield");
+    // sort decreasing
     TopDocs results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(onefield.getSortField(true)));
     FieldDoc first = (FieldDoc) results.scoreDocs[0];
-    assertEquals(45L, first.fields[0]);
+    assertEquals(LEAST_LONG_VALUE, first.fields[0]);
+
+    // sort increasing
+    results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(onefield.getSortField(false)));
+    first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(0L, first.fields[0]);
+  }
+
+  public void testSortMissingExplicit() throws Exception {
+    // docs w/no value get provided missing value
+
+    LongValuesSource onefield = LongValuesSource.fromLongField("onefield");
+
+    // sort decreasing, missing last
+    SortField oneFieldSort = onefield.getSortField(true);
+    oneFieldSort.setMissingValue(Long.MIN_VALUE);
+
+    TopDocs results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(oneFieldSort));
+    FieldDoc first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(LEAST_LONG_VALUE, first.fields[0]);
+
+    // sort increasing, missing last
+    oneFieldSort = onefield.getSortField(false);
+    oneFieldSort.setMissingValue(Long.MAX_VALUE);
+
+    results = searcher.search(new MatchAllDocsQuery(), 1, new Sort(oneFieldSort));
+    first = (FieldDoc) results.scoreDocs[0];
+    assertEquals(LEAST_LONG_VALUE, first.fields[0]);
   }
 
   public void testSimpleFieldEquivalences() throws Exception {