Skip to content

Apache Lucene open-source search software

add LUCENE-4776 patch and impl it for docTermOrds too

Robert Muir00d43161623e
diff --git a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java
index 76c5a3680a7..d44bb817c7b 100644
--- a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java
+++ b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java
@@ -29,6 +29,7 @@ import org.apache.lucene.util.BytesRef;
public class SingletonSortedSetDocValues extends SortedSetDocValues {
private final SortedDocValues in;
private int docID;
+ private boolean set;
/** Creates a multi-valued view over the provided SortedDocValues */
public SingletonSortedSetDocValues(SortedDocValues in) {
@@ -38,12 +39,18 @@ public class SingletonSortedSetDocValues extends SortedSetDocValues {
@Override
public long nextOrd() {
- return in.getOrd(docID);
+ if (set) {
+ return NO_MORE_ORDS;
+ } else {
+ set = true;
+ return in.getOrd(docID);
+ }
}
@Override
public void setDocument(int docID) {
this.docID = docID;
+ set = false;
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java b/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java
index c06204b2c69..346aff7c3e3 100644
--- a/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java
+++ b/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java
@@ -374,6 +374,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Bytes) caches.get(Byte.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -460,6 +464,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Shorts) caches.get(Short.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -544,6 +552,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Ints) caches.get(Integer.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -730,6 +742,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Floats) caches.get(Float.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -833,6 +849,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Longs) caches.get(Long.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -936,6 +956,10 @@ class FieldCacheImpl implements FieldCache {
}
};
} else {
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
return (Doubles) caches.get(Double.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);
}
}
@@ -1066,9 +1090,12 @@ class FieldCacheImpl implements FieldCache {
return valuesIn;
} else {
final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
- if (info != null && !info.isIndexed() && info.hasDocValues()) {
+ if (info != null && info.hasDocValues()) {
// we don't try to build a sorted instance from numeric/binary doc
// values because dedup can be very costly
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
+ if (info != null && !info.isIndexed()) {
throw new IllegalArgumentException("Cannot get terms index for \"" + field
+ "\": it isn't indexed and doesn't have sorted doc values");
}
@@ -1222,6 +1249,11 @@ class FieldCacheImpl implements FieldCache {
return valuesIn;
}
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
+
return (BinaryDocValues) caches.get(BinaryDocValues.class).get(reader, new CacheKey(field, acceptableOverheadRatio), false);
}
@@ -1318,6 +1350,11 @@ class FieldCacheImpl implements FieldCache {
return new SingletonSortedSetDocValues(sdv);
}
+ final FieldInfo info = reader.getFieldInfos().fieldInfo(field);
+ if (info != null && info.hasDocValues()) {
+ throw new IllegalStateException("Type mismatch: " + field + " was indexed as " + info.getDocValuesType());
+ }
+
DocTermOrds dto = (DocTermOrds) caches.get(DocTermOrds.class).get(reader, new CacheKey(field, null), false);
return dto.iterator(dto.getOrdTermsEnum(reader));
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java b/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java
index d566adb27ae..892e3f4aae6 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java
@@ -29,11 +29,17 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.codecs.DocValuesFormat;
+import org.apache.lucene.document.BinaryDocValuesField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.document.SortedDocValuesField;
+import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
+import org.apache.lucene.search.FieldCache.Ints;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
@@ -419,4 +425,124 @@ public class TestFieldCache extends LuceneTestCase {
}
assertFalse(failed.get());
}
+
+ public void testDocValuesIntegration() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, null);
+ // nocommit
+ iwc.setCodec(_TestUtil.alwaysDocValuesFormat(DocValuesFormat.forName("Asserting")));
+ RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
+ Document doc = new Document();
+ doc.add(new BinaryDocValuesField("binary", new BytesRef("binary value")));
+ doc.add(new SortedDocValuesField("sorted", new BytesRef("sorted value")));
+ doc.add(new NumericDocValuesField("numeric", 42));
+ doc.add(new SortedSetDocValuesField("sortedset", new BytesRef("sortedset value1")));
+ doc.add(new SortedSetDocValuesField("sortedset", new BytesRef("sortedset value2")));
+ iw.addDocument(doc);
+ DirectoryReader ir = iw.getReader();
+ iw.close();
+ AtomicReader ar = getOnlySegmentReader(ir);
+
+ BytesRef scratch = new BytesRef();
+
+ // Binary type: can be retrieved via getTerms()
+ try {
+ FieldCache.DEFAULT.getInts(ar, "binary", false);
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ BinaryDocValues binary = FieldCache.DEFAULT.getTerms(ar, "binary");
+ binary.get(0, scratch);
+ assertEquals("binary value", scratch.utf8ToString());
+
+ try {
+ FieldCache.DEFAULT.getTermsIndex(ar, "binary");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ try {
+ FieldCache.DEFAULT.getDocTermOrds(ar, "binary");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ Bits bits = FieldCache.DEFAULT.getDocsWithField(ar, "binary");
+ assertTrue(bits instanceof Bits.MatchAllBits);
+
+ // Sorted type: can be retrieved via getTerms(), getTermsIndex(), getDocTermOrds()
+ try {
+ FieldCache.DEFAULT.getInts(ar, "sorted", false);
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ binary = FieldCache.DEFAULT.getTerms(ar, "sorted");
+ binary.get(0, scratch);
+ assertEquals("sorted value", scratch.utf8ToString());
+
+ SortedDocValues sorted = FieldCache.DEFAULT.getTermsIndex(ar, "sorted");
+ assertEquals(0, sorted.getOrd(0));
+ assertEquals(1, sorted.getValueCount());
+ sorted.get(0, scratch);
+ assertEquals("sorted value", scratch.utf8ToString());
+
+ SortedSetDocValues sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sorted");
+ sortedSet.setDocument(0);
+ assertEquals(0, sortedSet.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSet.nextOrd());
+ assertEquals(1, sortedSet.getValueCount());
+
+ bits = FieldCache.DEFAULT.getDocsWithField(ar, "sorted");
+ assertTrue(bits instanceof Bits.MatchAllBits);
+
+ // Numeric type: can be retrieved via getInts() and so on
+ Ints numeric = FieldCache.DEFAULT.getInts(ar, "numeric", false);
+ assertEquals(42, numeric.get(0));
+
+ try {
+ FieldCache.DEFAULT.getTerms(ar, "numeric");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ try {
+ FieldCache.DEFAULT.getTermsIndex(ar, "numeric");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ try {
+ FieldCache.DEFAULT.getDocTermOrds(ar, "numeric");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ bits = FieldCache.DEFAULT.getDocsWithField(ar, "numeric");
+ assertTrue(bits instanceof Bits.MatchAllBits);
+
+ // SortedSet type: can be retrieved via getDocTermOrds()
+ try {
+ FieldCache.DEFAULT.getInts(ar, "sortedset", false);
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ try {
+ FieldCache.DEFAULT.getTerms(ar, "sortedset");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ try {
+ FieldCache.DEFAULT.getTermsIndex(ar, "sortedset");
+ fail();
+ } catch (IllegalStateException expected) {}
+
+ sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sortedset");
+ sortedSet.setDocument(0);
+ assertEquals(0, sortedSet.nextOrd());
+ assertEquals(1, sortedSet.nextOrd());
+ assertEquals(SortedSetDocValues.NO_MORE_ORDS, sortedSet.nextOrd());
+ assertEquals(2, sortedSet.getValueCount());
+
+ // nocommit: not right
+ bits = FieldCache.DEFAULT.getDocsWithField(ar, "sortedset");
+ assertTrue(bits instanceof Bits.MatchAllBits);
+
+ ir.close();
+ dir.close();
+ }
}