Apache Lucene open-source search software
LUCENE-3407: wrong stats from MemoryCodec when freqs are omitted
Robert Muir7c92a0d48d55
diff --git a/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java b/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.javaindex 273258fbd28..029f6cac08d 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java@@ -554,7 +554,7 @@ public class MemoryCodec extends Codec {if (field.indexOptions != IndexOptions.DOCS_ONLY) {totalTermFreq = docFreq + buffer.readVLong();} else {- totalTermFreq = 0;+ totalTermFreq = -1;}current.output.offset = buffer.getPosition();if (VERBOSE) System.out.println(" df=" + docFreq + " totTF=" + totalTermFreq + " offset=" + buffer.getPosition() + " len=" + current.output.length);@@ -692,7 +692,7 @@ public class MemoryCodec extends Codec {if (field.indexOptions != IndexOptions.DOCS_ONLY) {sumTotalTermFreq = in.readVLong();} else {- sumTotalTermFreq = 0;+ sumTotalTermFreq = -1;}sumDocFreq = in.readVLong();diff --git a/lucene/src/test/org/apache/lucene/index/TestOmitTf.java b/lucene/src/test/org/apache/lucene/index/TestOmitTf.javaindex 1356fd8ddf7..92e522aef90 100644--- a/lucene/src/test/org/apache/lucene/index/TestOmitTf.java+++ b/lucene/src/test/org/apache/lucene/index/TestOmitTf.java@@ -428,4 +428,25 @@ public class TestOmitTf extends LuceneTestCase {return true;}}++ /** test that when freqs are omitted, that totalTermFreq and sumTotalTermFreq are -1 */+ public void testStats() throws Exception {+ Directory dir = newDirectory();+ RandomIndexWriter iw = new RandomIndexWriter(random, dir,+ newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));+ Document doc = new Document();+ FieldType ft = new FieldType(TextField.TYPE_UNSTORED);+ ft.setIndexOptions(IndexOptions.DOCS_ONLY);+ ft.freeze();+ Field f = newField("foo", "bar", ft);+ doc.add(f);+ iw.addDocument(doc);+ IndexReader ir = iw.getReader();+ iw.close();+ Terms terms = MultiFields.getTerms(ir, "foo");+ assertEquals(-1, terms.totalTermFreq(new BytesRef("bar")));+ assertEquals(-1, terms.getSumTotalTermFreq());+ ir.close();+ dir.close();+ }}