Skip to content

Apache Lucene open-source search software

clarify DocsEnum.freq javadocs + add test

Shai Ererae25e019c1dd8
diff --git a/lucene/core/src/java/org/apache/lucene/index/DocsEnum.java b/lucene/core/src/java/org/apache/lucene/index/DocsEnum.java
index f900e533ded..fa4cf54679f 100644
--- a/lucene/core/src/java/org/apache/lucene/index/DocsEnum.java
+++ b/lucene/core/src/java/org/apache/lucene/index/DocsEnum.java
@@ -19,6 +19,7 @@ package org.apache.lucene.index;
import java.io.IOException;
+import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.Bits; // javadocs
@@ -47,10 +48,16 @@ public abstract class DocsEnum extends DocIdSetIterator {
protected DocsEnum() {
}
- /** Returns term frequency in the current document. Do
- * not call this before {@link #nextDoc} is first called,
- * nor after {@link #nextDoc} returns NO_MORE_DOCS.
- **/
+ /**
+ * Returns term frequency in the current document, or 1 if the field was
+ * indexed with {@link IndexOptions#DOCS_ONLY}. Do not call this before
+ * {@link #nextDoc} is first called, nor after {@link #nextDoc} returns
+ * {@link DocIdSetIterator#NO_MORE_DOCS}.
+ *
+ * <p>
+ * <b>NOTE:</b> if the {@link DocsEnum} was obtain with {@link #FLAG_NONE},
+ * the result of this method is undefined.
+ */
public abstract int freq() throws IOException;
/** Returns the related attributes. */
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java
index 8b49049e888..af1b75a5382 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.codecs.Codec;
@@ -31,7 +32,9 @@ import org.apache.lucene.codecs.TermStats;
import org.apache.lucene.codecs.TermsConsumer;
import org.apache.lucene.codecs.mocksep.MockSepPostingsFormat;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.DocIdSetIterator;
@@ -630,4 +633,33 @@ public class TestCodecs extends LuceneTestCase {
}
consumer.close();
}
+
+ public void testDocsOnlyFreq() throws Exception {
+ // tests that when fields are indexed with DOCS_ONLY, the Codec
+ // returns 1 in docsEnum.freq()
+ Directory dir = newDirectory();
+ Random random = random();
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
+ // we don't need many documents to assert this, but don't use one document either
+ int numDocs = atLeast(random, 50);
+ for (int i = 0; i < numDocs; i++) {
+ Document doc = new Document();
+ doc.add(new StringField("f", "doc", Store.NO));
+ writer.addDocument(doc);
+ }
+ writer.close();
+
+ Term term = new Term("f", new BytesRef("doc"));
+ DirectoryReader reader = DirectoryReader.open(dir);
+ for (AtomicReaderContext ctx : reader.leaves()) {
+ DocsEnum de = ctx.reader().termDocsEnum(term);
+ while (de.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+ assertEquals("wrong freq for doc " + de.docID(), 1, de.freq());
+ }
+ }
+ reader.close();
+
+ dir.close();
+ }
}