Skip to content

Apache Lucene open-source search software

LUCENE-5666: javadocs, support SORTED_SET for multi-valued numerics

Robert Muir5aba5fda6757
diff --git a/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java b/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
index 4559fd86f9c..2015e4f2922 100644
--- a/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
+++ b/lucene/misc/src/java/org/apache/lucene/uninverting/DocTermOrds.java
@@ -185,6 +185,8 @@ public class DocTermOrds {
public DocTermOrds(AtomicReader reader, Bits liveDocs, String field) throws IOException {
this(reader, liveDocs, field, null, Integer.MAX_VALUE);
}
+
+ // TODO: instead of all these ctors and options, take termsenum!
/** Inverts only terms starting w/ prefix */
public DocTermOrds(AtomicReader reader, Bits liveDocs, String field, BytesRef termPrefix) throws IOException {
diff --git a/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCache.java b/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCache.java
index 665653972df..efe23c95316 100644
--- a/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCache.java
+++ b/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCache.java
@@ -246,10 +246,12 @@ interface FieldCache {
*
* @param reader Used to build a {@link DocTermOrds} instance
* @param field Which field contains the strings.
+ * @param prefix prefix for a subset of the terms which should be uninverted. Can be null.
* @return a {@link DocTermOrds} instance
* @throws IOException If any error occurs.
*/
- public SortedSetDocValues getDocTermOrds(AtomicReader reader, String field) throws IOException;
+ // TODO: change this to take Parser
+ public SortedSetDocValues getDocTermOrds(AtomicReader reader, String field, BytesRef prefix) throws IOException;
/**
* EXPERT: A unique Identifier/Description for each item in the FieldCache.
diff --git a/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCacheImpl.java b/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCacheImpl.java
index 0bfca7a641e..d848e60ff0c 100644
--- a/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCacheImpl.java
+++ b/lucene/misc/src/java/org/apache/lucene/uninverting/FieldCacheImpl.java
@@ -840,7 +840,7 @@ class FieldCacheImpl implements FieldCache {
// TODO: this if DocTermsIndex was already created, we
// should share it...
- public SortedSetDocValues getDocTermOrds(AtomicReader reader, String field) throws IOException {
+ public SortedSetDocValues getDocTermOrds(AtomicReader reader, String field, BytesRef prefix) throws IOException {
SortedSetDocValues dv = reader.getSortedSetDocValues(field);
if (dv != null) {
return dv;
@@ -860,7 +860,7 @@ class FieldCacheImpl implements FieldCache {
return DocValues.EMPTY_SORTED_SET;
}
- DocTermOrds dto = (DocTermOrds) caches.get(DocTermOrds.class).get(reader, new CacheKey(field, null), false);
+ DocTermOrds dto = (DocTermOrds) caches.get(DocTermOrds.class).get(reader, new CacheKey(field, prefix), false);
return dto.iterator(reader);
}
@@ -872,7 +872,8 @@ class FieldCacheImpl implements FieldCache {
@Override
protected Object createValue(AtomicReader reader, CacheKey key, boolean setDocsWithField /* ignored */)
throws IOException {
- return new DocTermOrds(reader, null, key.field);
+ BytesRef prefix = (BytesRef) key.custom;
+ return new DocTermOrds(reader, null, key.field, prefix);
}
}
diff --git a/lucene/misc/src/java/org/apache/lucene/uninverting/UninvertingReader.java b/lucene/misc/src/java/org/apache/lucene/uninverting/UninvertingReader.java
index 5480db3c851..1d7dcaaa388 100644
--- a/lucene/misc/src/java/org/apache/lucene/uninverting/UninvertingReader.java
+++ b/lucene/misc/src/java/org/apache/lucene/uninverting/UninvertingReader.java
@@ -21,6 +21,15 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
+import org.apache.lucene.document.IntField; // javadocs
+import org.apache.lucene.document.LongField; // javadocs
+import org.apache.lucene.document.FloatField; // javadocs
+import org.apache.lucene.document.DoubleField; // javadocs
+import org.apache.lucene.document.BinaryDocValuesField; // javadocs
+import org.apache.lucene.document.NumericDocValuesField; // javadocs
+import org.apache.lucene.document.SortedDocValuesField; // javadocs
+import org.apache.lucene.document.SortedSetDocValuesField; // javadocs
+import org.apache.lucene.document.StringField; // javadocs
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DirectoryReader;
@@ -32,19 +41,110 @@ import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.NumericUtils;
+/**
+ * A FilterReader that exposes <i>indexed</i> values as if they also had
+ * docvalues.
+ * <p>
+ * This is accomplished by "inverting the inverted index" or "uninversion".
+ * <p>
+ * The uninversion process happens lazily: upon the first request for the
+ * field's docvalues (e.g. via {@link AtomicReader#getNumericDocValues(String)}
+ * or similar), it will create the docvalues on-the-fly if needed and cache it,
+ * based on the core cache key of the wrapped AtomicReader.
+ */
public class UninvertingReader extends FilterAtomicReader {
+ /**
+ * Specifies the type of uninversion to apply for the field.
+ */
public static enum Type {
+ /**
+ * Single-valued Integer, (e.g. indexed with {@link IntField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link NumericDocValuesField}.
+ */
INTEGER,
+ /**
+ * Single-valued Long, (e.g. indexed with {@link LongField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link NumericDocValuesField}.
+ */
LONG,
+ /**
+ * Single-valued Float, (e.g. indexed with {@link FloatField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link NumericDocValuesField}.
+ */
FLOAT,
+ /**
+ * Single-valued Double, (e.g. indexed with {@link DoubleField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link NumericDocValuesField}.
+ */
DOUBLE,
+ /**
+ * Single-valued Binary, (e.g. indexed with {@link StringField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link BinaryDocValuesField}.
+ */
BINARY,
+ /**
+ * Single-valued Binary, (e.g. indexed with {@link StringField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedDocValuesField}.
+ */
SORTED,
- SORTED_SET
+ /**
+ * Multi-valued Binary, (e.g. indexed with {@link StringField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedSetDocValuesField}.
+ */
+ SORTED_SET_BINARY,
+ /**
+ * Multi-valued Integer, (e.g. indexed with {@link IntField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedSetDocValuesField}.
+ */
+ SORTED_SET_INTEGER,
+ /**
+ * Multi-valued Float, (e.g. indexed with {@link FloatField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedSetDocValuesField}.
+ */
+ SORTED_SET_FLOAT,
+ /**
+ * Multi-valued Long, (e.g. indexed with {@link LongField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedSetDocValuesField}.
+ */
+ SORTED_SET_LONG,
+ /**
+ * Multi-valued Double, (e.g. indexed with {@link DoubleField})
+ * <p>
+ * Fields with this type act as if they were indexed with
+ * {@link SortedSetDocValuesField}.
+ */
+ SORTED_SET_DOUBLE
}
+ /**
+ * Wraps a provided DirectoryReader. Note that for convenience, the returned reader
+ * can be used normally (e.g. passed to {@link DirectoryReader#openIfChanged(DirectoryReader)})
+ * and so on.
+ */
public static DirectoryReader wrap(DirectoryReader in, final Map<String,Type> mapping) {
return new UninvertingDirectoryReader(in, mapping);
}
@@ -93,7 +193,11 @@ public class UninvertingReader extends FilterAtomicReader {
case SORTED:
type = FieldInfo.DocValuesType.SORTED;
break;
- case SORTED_SET:
+ case SORTED_SET_BINARY:
+ case SORTED_SET_INTEGER:
+ case SORTED_SET_FLOAT:
+ case SORTED_SET_LONG:
+ case SORTED_SET_DOUBLE:
type = FieldInfo.DocValuesType.SORTED_SET;
break;
default:
@@ -143,14 +247,27 @@ public class UninvertingReader extends FilterAtomicReader {
return in.getSortedDocValues(field);
}
}
+
+ // TODO: clean this up to instead just pass parsers...
+ static final BytesRef INT32_TERM_PREFIX = new BytesRef(new byte[] { NumericUtils.SHIFT_START_INT });
+ static final BytesRef INT64_TERM_PREFIX = new BytesRef(new byte[] { NumericUtils.SHIFT_START_LONG });
@Override
public SortedSetDocValues getSortedSetDocValues(String field) throws IOException {
- if (mapping.get(field) == Type.SORTED_SET) {
- return FieldCache.DEFAULT.getDocTermOrds(in, field);
- } else {
- return in.getSortedSetDocValues(field);
+ Type v = mapping.get(field);
+ if (v != null) {
+ switch (mapping.get(field)) {
+ case SORTED_SET_INTEGER:
+ case SORTED_SET_FLOAT:
+ return FieldCache.DEFAULT.getDocTermOrds(in, field, INT32_TERM_PREFIX);
+ case SORTED_SET_LONG:
+ case SORTED_SET_DOUBLE:
+ return FieldCache.DEFAULT.getDocTermOrds(in, field, INT64_TERM_PREFIX);
+ case SORTED_SET_BINARY:
+ return FieldCache.DEFAULT.getDocTermOrds(in, field, null);
+ }
}
+ return in.getSortedSetDocValues(field);
}
@Override
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
index 766b0f442d8..0a4b67b4ee4 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestDocTermOrds.java
@@ -31,6 +31,7 @@ import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
+import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReaderContext;
@@ -50,6 +51,7 @@ import org.apache.lucene.index.TermsEnum.SeekStatus;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.TestUtil;
@@ -407,9 +409,9 @@ public class TestDocTermOrds extends LuceneTestCase {
iw.deleteDocuments(new Term("foo", "baz"));
DirectoryReader r2 = DirectoryReader.open(iw, true);
- FieldCache.DEFAULT.getDocTermOrds(getOnlySegmentReader(r2), "foo");
+ FieldCache.DEFAULT.getDocTermOrds(getOnlySegmentReader(r2), "foo", null);
- SortedSetDocValues v = FieldCache.DEFAULT.getDocTermOrds(getOnlySegmentReader(r1), "foo");
+ SortedSetDocValues v = FieldCache.DEFAULT.getDocTermOrds(getOnlySegmentReader(r1), "foo", null);
assertEquals(2, v.getValueCount());
v.setDocument(1);
assertEquals(1, v.nextOrd());
@@ -420,6 +422,90 @@ public class TestDocTermOrds extends LuceneTestCase {
dir.close();
}
+ public void testNumericEncoded32() throws IOException {
+ Directory dir = newDirectory();
+ IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, null));
+
+ Document doc = new Document();
+ doc.add(new IntField("foo", 5, Field.Store.NO));
+ iw.addDocument(doc);
+
+ doc = new Document();
+ doc.add(new IntField("foo", 5, Field.Store.NO));
+ doc.add(new IntField("foo", -3, Field.Store.NO));
+ iw.addDocument(doc);
+
+ iw.forceMerge(1);
+ iw.shutdown();
+
+ DirectoryReader ir = DirectoryReader.open(dir);
+ AtomicReader ar = getOnlySegmentReader(ir);
+
+ SortedSetDocValues v = FieldCache.DEFAULT.getDocTermOrds(ar, "foo", UninvertingReader.INT32_TERM_PREFIX);
+ assertEquals(2, v.getValueCount());
+
+ v.setDocument(0);
+ assertEquals(1, v.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, v.nextOrd());
+
+ v.setDocument(1);
+ assertEquals(0, v.nextOrd());
+ assertEquals(1, v.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, v.nextOrd());
+
+ BytesRef value = new BytesRef();
+ v.lookupOrd(0, value);
+ assertEquals(-3, NumericUtils.prefixCodedToInt(value));
+
+ v.lookupOrd(1, value);
+ assertEquals(5, NumericUtils.prefixCodedToInt(value));
+
+ ir.close();
+ dir.close();
+ }
+
+ public void testNumericEncoded64() throws IOException {
+ Directory dir = newDirectory();
+ IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, null));
+
+ Document doc = new Document();
+ doc.add(new LongField("foo", 5, Field.Store.NO));
+ iw.addDocument(doc);
+
+ doc = new Document();
+ doc.add(new LongField("foo", 5, Field.Store.NO));
+ doc.add(new LongField("foo", -3, Field.Store.NO));
+ iw.addDocument(doc);
+
+ iw.forceMerge(1);
+ iw.shutdown();
+
+ DirectoryReader ir = DirectoryReader.open(dir);
+ AtomicReader ar = getOnlySegmentReader(ir);
+
+ SortedSetDocValues v = FieldCache.DEFAULT.getDocTermOrds(ar, "foo", UninvertingReader.INT64_TERM_PREFIX);
+ assertEquals(2, v.getValueCount());
+
+ v.setDocument(0);
+ assertEquals(1, v.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, v.nextOrd());
+
+ v.setDocument(1);
+ assertEquals(0, v.nextOrd());
+ assertEquals(1, v.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, v.nextOrd());
+
+ BytesRef value = new BytesRef();
+ v.lookupOrd(0, value);
+ assertEquals(-3, NumericUtils.prefixCodedToLong(value));
+
+ v.lookupOrd(1, value);
+ assertEquals(5, NumericUtils.prefixCodedToLong(value));
+
+ ir.close();
+ dir.close();
+ }
+
public void testSortedTermsEnum() throws IOException {
Directory directory = newDirectory();
Analyzer analyzer = new MockAnalyzer(random());
@@ -444,7 +530,7 @@ public class TestDocTermOrds extends LuceneTestCase {
iwriter.shutdown();
AtomicReader ar = getOnlySegmentReader(ireader);
- SortedSetDocValues dv = FieldCache.DEFAULT.getDocTermOrds(ar, "field");
+ SortedSetDocValues dv = FieldCache.DEFAULT.getDocTermOrds(ar, "field", null);
assertEquals(3, dv.getValueCount());
TermsEnum termsEnum = dv.termsEnum();
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
index 9ec2b2041f1..b15e9330988 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
@@ -265,10 +265,10 @@ public class TestFieldCache extends LuceneTestCase {
terms = cache.getTerms(reader, "bogusfield", false);
// getDocTermOrds
- SortedSetDocValues termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField");
+ SortedSetDocValues termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField", null);
int numEntries = cache.getCacheEntries().length;
// ask for it again, and check that we didnt create any additional entries:
- termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField");
+ termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField", null);
assertEquals(numEntries, cache.getCacheEntries().length);
for (int i = 0; i < NUM_DOCS; i++) {
@@ -290,7 +290,7 @@ public class TestFieldCache extends LuceneTestCase {
}
// test bad field
- termOrds = cache.getDocTermOrds(reader, "bogusfield");
+ termOrds = cache.getDocTermOrds(reader, "bogusfield", null);
assertTrue(termOrds.getValueCount() == 0);
FieldCache.DEFAULT.purgeByCacheKey(reader.getCoreCacheKey());
@@ -466,7 +466,7 @@ public class TestFieldCache extends LuceneTestCase {
} catch (IllegalStateException expected) {}
try {
- FieldCache.DEFAULT.getDocTermOrds(ar, "binary");
+ FieldCache.DEFAULT.getDocTermOrds(ar, "binary", null);
fail();
} catch (IllegalStateException expected) {}
@@ -499,7 +499,7 @@ public class TestFieldCache extends LuceneTestCase {
sorted.get(0, scratch);
assertEquals("sorted value", scratch.utf8ToString());
- SortedSetDocValues sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sorted");
+ SortedSetDocValues sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sorted", null);
sortedSet.setDocument(0);
assertEquals(0, sortedSet.nextOrd());
assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSet.nextOrd());
@@ -523,7 +523,7 @@ public class TestFieldCache extends LuceneTestCase {
} catch (IllegalStateException expected) {}
try {
- FieldCache.DEFAULT.getDocTermOrds(ar, "numeric");
+ FieldCache.DEFAULT.getDocTermOrds(ar, "numeric", null);
fail();
} catch (IllegalStateException expected) {}
@@ -557,7 +557,7 @@ public class TestFieldCache extends LuceneTestCase {
fail();
} catch (IllegalStateException expected) {}
- sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sortedset");
+ sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sortedset", null);
sortedSet.setDocument(0);
assertEquals(0, sortedSet.nextOrd());
assertEquals(1, sortedSet.nextOrd());
@@ -608,7 +608,7 @@ public class TestFieldCache extends LuceneTestCase {
sorted.get(0, scratch);
assertEquals(0, scratch.length);
- SortedSetDocValues sortedSet = cache.getDocTermOrds(ar, "bogusmultivalued");
+ SortedSetDocValues sortedSet = cache.getDocTermOrds(ar, "bogusmultivalued", null);
sortedSet.setDocument(0);
assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSet.nextOrd());
@@ -667,7 +667,7 @@ public class TestFieldCache extends LuceneTestCase {
sorted.get(0, scratch);
assertEquals(0, scratch.length);
- SortedSetDocValues sortedSet = cache.getDocTermOrds(ar, "bogusmultivalued");
+ SortedSetDocValues sortedSet = cache.getDocTermOrds(ar, "bogusmultivalued", null);
sortedSet.setDocument(0);
assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSet.nextOrd());
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheVsDocValues.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheVsDocValues.java
index cf59b051f3e..12844704b37 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheVsDocValues.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheVsDocValues.java
@@ -389,7 +389,7 @@ public class TestFieldCacheVsDocValues extends LuceneTestCase {
DirectoryReader ir = writer.getReader();
for (AtomicReaderContext context : ir.leaves()) {
AtomicReader r = context.reader();
- SortedSetDocValues expected = FieldCache.DEFAULT.getDocTermOrds(r, "indexed");
+ SortedSetDocValues expected = FieldCache.DEFAULT.getDocTermOrds(r, "indexed", null);
SortedSetDocValues actual = r.getSortedSetDocValues("dv");
assertEquals(r.maxDoc(), expected, actual);
}
@@ -400,7 +400,7 @@ public class TestFieldCacheVsDocValues extends LuceneTestCase {
// now compare again after the merge
ir = writer.getReader();
AtomicReader ar = getOnlySegmentReader(ir);
- SortedSetDocValues expected = FieldCache.DEFAULT.getDocTermOrds(ar, "indexed");
+ SortedSetDocValues expected = FieldCache.DEFAULT.getDocTermOrds(ar, "indexed", null);
SortedSetDocValues actual = ar.getSortedSetDocValues("dv");
assertEquals(ir.maxDoc(), expected, actual);
ir.close();