Apache Solr open-source search software
LUCENE-5772: implement getSortedNumericDocValues in SortingAtomicReader
Shai Erera2bac24232986
diff --git a/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.javaindex 9157919ae14..e81dea7b92c 100644--- a/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java+++ b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java@@ -30,10 +30,12 @@ import org.apache.lucene.index.Fields;import org.apache.lucene.index.FilterAtomicReader;import org.apache.lucene.index.NumericDocValues;import org.apache.lucene.index.SortedDocValues;+import org.apache.lucene.index.SortedNumericDocValues;import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.index.StoredFieldVisitor;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;+import org.apache.lucene.index.sorter.Sorter.DocMap;import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.Sort;import org.apache.lucene.store.IndexInput;@@ -222,6 +224,32 @@ public class SortingAtomicReader extends FilterAtomicReader {}}+ private static class SortingSortedNumericDocValues extends SortedNumericDocValues {++ private final SortedNumericDocValues in;+ private final Sorter.DocMap docMap;++ SortingSortedNumericDocValues(SortedNumericDocValues in, DocMap docMap) {+ this.in = in;+ this.docMap = docMap;+ }++ @Override+ public int count() {+ return in.count();+ }++ @Override+ public void setDocument(int doc) {+ in.setDocument(docMap.newToOld(doc));+ }++ @Override+ public long valueAt(int index) {+ return in.valueAt(index);+ }+ }+private static class SortingBits implements Bits {private final Bits in;@@ -786,6 +814,17 @@ public class SortingAtomicReader extends FilterAtomicReader {if (oldDocValues == null) return null;return new SortingNumericDocValues(oldDocValues, docMap);}++ @Override+ public SortedNumericDocValues getSortedNumericDocValues(String field)+ throws IOException {+ final SortedNumericDocValues oldDocValues = in.getSortedNumericDocValues(field);+ if (oldDocValues == null) {+ return null;+ } else {+ return new SortingSortedNumericDocValues(oldDocValues, docMap);+ }+ }@Overridepublic SortedDocValues getSortedDocValues(String field) throws IOException {diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.javaindex 120d5e76f57..484f09ec186 100644--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java@@ -36,6 +36,7 @@ import org.apache.lucene.document.Field.Store;import org.apache.lucene.document.FieldType;import org.apache.lucene.document.NumericDocValuesField;import org.apache.lucene.document.SortedDocValuesField;+import org.apache.lucene.document.SortedNumericDocValuesField;import org.apache.lucene.document.SortedSetDocValuesField;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;@@ -52,6 +53,7 @@ import org.apache.lucene.index.NumericDocValues;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.SlowCompositeReaderWrapper;import org.apache.lucene.index.SortedDocValues;+import org.apache.lucene.index.SortedNumericDocValues;import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.index.Term;import org.apache.lucene.index.Terms;@@ -145,6 +147,7 @@ public abstract class SorterTestBase extends LuceneTestCase {protected static final String DOC_POSITIONS_FIELD = "positions";protected static final String DOC_POSITIONS_TERM = "$all$";protected static final String NUMERIC_DV_FIELD = "numeric";+ protected static final String SORTED_NUMERIC_DV_FIELD = "sorted_numeric";protected static final String NORMS_FIELD = "norm";protected static final String BINARY_DV_FIELD = "binary";protected static final String SORTED_DV_FIELD = "sorted";@@ -183,6 +186,10 @@ public abstract class SorterTestBase extends LuceneTestCase {doc.add(new SortedSetDocValuesField(SORTED_SET_DV_FIELD, new BytesRef(Integer.toString(id))));doc.add(new SortedSetDocValuesField(SORTED_SET_DV_FIELD, new BytesRef(Integer.toString(id + 1))));}+ if (defaultCodecSupportsSortedNumeric()) {+ doc.add(new SortedNumericDocValuesField(SORTED_NUMERIC_DV_FIELD, id));+ doc.add(new SortedNumericDocValuesField(SORTED_NUMERIC_DV_FIELD, id + 1));+ }doc.add(new Field(TERM_VECTORS_FIELD, Integer.toString(id), TERM_VECTORS_TYPE));return doc;}@@ -388,6 +395,20 @@ public abstract class SorterTestBase extends LuceneTestCase {}}+ @Test+ public void testSortedNumericDocValuesField() throws Exception {+ assumeTrue("default codec does not support SORTED_NUMERIC", defaultCodecSupportsSortedNumeric());+ SortedNumericDocValues dv = reader.getSortedNumericDocValues(SORTED_NUMERIC_DV_FIELD);+ int maxDoc = reader.maxDoc();+ for (int i = 0; i < maxDoc; i++) {+ dv.setDocument(i);+ assertEquals(2, dv.count());+ int value = sortedValues[i].intValue();+ assertEquals("incorrect sorted-numeric DocValues for doc " + i, value, dv.valueAt(0));+ assertEquals("incorrect sorted-numeric DocValues for doc " + i, value + 1, dv.valueAt(1));+ }+ }+@Testpublic void testTermVectors() throws Exception {int maxDoc = reader.maxDoc();