clarify DocsEnum.freq javadocs + add test

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1423846 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2012-12-19 12:51:16 +00:00
parent 0fbb260f1e
commit e25e019c1d
2 changed files with 43 additions and 4 deletions

View File

@ -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. */

View File

@ -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();
}
}