You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2013/04/10 12:56:39 UTC

svn commit: r1466422 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/misc/ lucene/misc/src/java/org/apache/lucene/index/sorter/ lucene/misc/src/test/org/apache/lucene/index/sorter/

Author: shaie
Date: Wed Apr 10 10:56:38 2013
New Revision: 1466422

URL: http://svn.apache.org/r1466422
Log:
LUCENE-4904: add descending sort order to NumericDocValuesSorter

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_4x/lucene/misc/   (props changed)
    lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java
    lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java
    lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java

Modified: lucene/dev/branches/branch_4x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/CHANGES.txt?rev=1466422&r1=1466421&r2=1466422&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/lucene/CHANGES.txt Wed Apr 10 10:56:38 2013
@@ -150,6 +150,8 @@ New Features
   with SortingMergePolicy, which allows to early terminate queries on sorted
   indexes, when the sort order matches the index order. (Adrien Grand, Shai Erera)
 
+* LUCENE-4904: Added descending sort order to NumericDocValuesSorter. (Shai Erera)
+
 Optimizations
 
 * LUCENE-4839: SorterTemplate.merge can now be overridden in order to replace

Modified: lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java?rev=1466422&r1=1466421&r2=1466422&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java (original)
+++ lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java Wed Apr 10 10:56:38 2013
@@ -24,37 +24,58 @@ import org.apache.lucene.index.NumericDo
 
 /**
  * A {@link Sorter} which sorts documents according to their
- * {@link NumericDocValues}.
- *
+ * {@link NumericDocValues}. One can specify ascending or descending sort order.
+ * 
  * @lucene.experimental
  */
 public class NumericDocValuesSorter extends Sorter {
 
   private final String fieldName;
-
+  private final boolean ascending;
+  
+  /** Constructor over the given field name, and ascending sort order. */
   public NumericDocValuesSorter(final String fieldName) {
+    this(fieldName, true);
+  }
+  
+  /**
+   * Constructor over the given field name, and whether sorting should be
+   * ascending ({@code true}) or descending ({@code false}).
+   */
+  public NumericDocValuesSorter(final String fieldName, boolean ascending) {
     this.fieldName = fieldName;
+    this.ascending = ascending;
   }
 
   @Override
   public Sorter.DocMap sort(final AtomicReader reader) throws IOException {
     final NumericDocValues ndv = reader.getNumericDocValues(fieldName);
-    final DocComparator comparator = new DocComparator() {
-
-      @Override
-      public int compare(int docID1, int docID2) {
-        final long v1 = ndv.get(docID1);
-        final long v2 = ndv.get(docID2);
-        return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;
-      }
-      
-    };
+    final DocComparator comparator;
+    if (ascending) {
+      comparator = new DocComparator() {
+        @Override
+        public int compare(int docID1, int docID2) {
+          final long v1 = ndv.get(docID1);
+          final long v2 = ndv.get(docID2);
+          return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;
+        }
+      };
+    } else {
+      comparator = new DocComparator() {
+        @Override
+        public int compare(int docID1, int docID2) {
+          final long v1 = ndv.get(docID1);
+          final long v2 = ndv.get(docID2);
+          return v1 > v2 ? -1 : v1 == v2 ? 0 : 1;
+        }
+      };
+    }
     return sort(reader.maxDoc(), comparator);
   }
   
   @Override
   public String getID() {
-    return "DocValues(" + fieldName + ",asc)";
+    return "DocValues(" + fieldName + "," + (ascending ? "ascending" : "descending") + ")";
   }
   
 }

Modified: lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java?rev=1466422&r1=1466421&r2=1466422&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java (original)
+++ lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/Sorter.java Wed Apr 10 10:56:38 2013
@@ -81,11 +81,12 @@ public abstract class Sorter {
 
   }
 
-  /** Sorts documents in reverse order.
-   *  <b>NOTE</b>: This {@link Sorter} is not idempotent. Sorting an
-   *  {@link AtomicReader} once or twice will return two different
-   *  {@link AtomicReader} views. This {@link Sorter} should not be used with
-   *  {@link SortingMergePolicy}. */
+  /**
+   * Sorts documents in reverse order. <b>NOTE</b>: This {@link Sorter} is not
+   * idempotent. Sorting an {@link AtomicReader} once or twice will return two
+   * different {@link AtomicReader} views. This {@link Sorter} should not be
+   * used with {@link SortingMergePolicy}.
+   */
   public static final Sorter REVERSE_DOCS = new Sorter() {
     @Override
     public DocMap sort(final AtomicReader reader) throws IOException {
@@ -111,7 +112,7 @@ public abstract class Sorter {
       return "ReverseDocs";
     }
   };
-
+  
   private static final class DocValueSorterTemplate extends SorterTemplate {
     
     private final int[] docs;
@@ -231,4 +232,9 @@ public abstract class Sorter {
    */
   public abstract String getID();
 
+  @Override
+  public String toString() {
+    return getID();
+  }
+  
 }

Modified: lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java?rev=1466422&r1=1466421&r2=1466422&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java (original)
+++ lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java Wed Apr 10 10:56:38 2013
@@ -32,7 +32,7 @@ import org.junit.BeforeClass;
 public class IndexSortingTest extends SorterTestBase {
   
   private static final Sorter[] SORTERS = new Sorter[] {
-    new NumericDocValuesSorter(NUMERIC_DV_FIELD),
+    new NumericDocValuesSorter(NUMERIC_DV_FIELD, true),
     Sorter.REVERSE_DOCS,
   };
   
@@ -52,6 +52,10 @@ public class IndexSortingTest extends So
       Collections.reverse(values);
     } else {
       Collections.sort(values);
+      if (sorter instanceof NumericDocValuesSorter && random().nextBoolean()) {
+        sorter = new NumericDocValuesSorter(NUMERIC_DV_FIELD, false); // descending
+        Collections.reverse(values);
+      }
     }
     sortedValues = values.toArray(new Integer[values.size()]);
     if (VERBOSE) {