From 7c92a0d48d556eb9d609d22f09613f6f053c021d Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Tue, 30 Aug 2011 13:58:06 +0000 Subject: [PATCH] LUCENE-3407: wrong stats from MemoryCodec when freqs are omitted git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1163213 13f79535-47bb-0310-9956-ffa450edef68 --- .../index/codecs/memory/MemoryCodec.java | 4 ++-- .../org/apache/lucene/index/TestOmitTf.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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.java index 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.java index 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(); + } }