Skip to content

Apache Lucene open-source search software

LUCENE-4485: CheckIndex's terms, terms/docs pairs counts don't include deleted docs

Michael McCandlesscd6c2fe0a2ed
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index d9863e204f4..421f6e9ebca 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -57,6 +57,9 @@ Bug Fixes
* LUCENE-4468: Fix rareish integer overflows in Block and Lucene40 postings
formats (Robert Muir)
+* LUCENE-4485: When CheckIndex terms, terms/docs pairs and tokens,
+ these counts now all exclude deleted documents. (Mike McCandless)
+
Optimizations
* LUCENE-4443: BlockPostingsFormat no longer writes unnecessary offsets
diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
index bf5df24a0a2..371c48990ab 100644
--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
@@ -233,9 +233,12 @@ public class CheckIndex {
TermIndexStatus() {
}
- /** Total term count */
+ /** Number of terms with at least one live doc. */
public long termCount = 0L;
+ /** Number of terms with zero live docs docs. */
+ public long delTermCount = 0L;
+
/** Total frequency across all terms. */
public long totFreq = 0L;
@@ -750,7 +753,7 @@ public class CheckIndex {
final TermsEnum termsEnum = terms.iterator(null);
boolean hasOrd = true;
- final long termCountStart = status.termCount;
+ final long termCountStart = status.delTermCount + status.termCount;
BytesRef lastTerm = null;
@@ -781,7 +784,6 @@ public class CheckIndex {
if (docFreq <= 0) {
throw new RuntimeException("docfreq: " + docFreq + " is out of bounds");
}
- status.totFreq += docFreq;
sumDocFreq += docFreq;
docs = termsEnum.docs(liveDocs, docs);
@@ -796,15 +798,13 @@ public class CheckIndex {
}
if (hasOrd) {
- final long ordExpected = status.termCount - termCountStart;
+ final long ordExpected = status.delTermCount + status.termCount - termCountStart;
if (ord != ordExpected) {
throw new RuntimeException("ord mismatch: TermsEnum has ord=" + ord + " vs actual=" + ordExpected);
}
}
}
- status.termCount++;
-
final DocsEnum docs2;
if (postings != null) {
docs2 = postings;
@@ -820,6 +820,7 @@ public class CheckIndex {
if (doc == DocIdSetIterator.NO_MORE_DOCS) {
break;
}
+ status.totFreq++;
visitedDocs.set(doc);
int freq = -1;
if (hasFreqs) {
@@ -883,6 +884,12 @@ public class CheckIndex {
}
}
+ if (docCount != 0) {
+ status.termCount++;
+ } else {
+ status.delTermCount++;
+ }
+
final long totalTermFreq2 = termsEnum.totalTermFreq();
final boolean hasTotalTermFreq = hasFreqs && totalTermFreq2 != -1;
@@ -1063,11 +1070,11 @@ public class CheckIndex {
// check unique term count
long termCount = -1;
- if (status.termCount-termCountStart > 0) {
+ if ((status.delTermCount+status.termCount)-termCountStart > 0) {
termCount = fields.terms(field).size();
- if (termCount != -1 && termCount != status.termCount - termCountStart) {
- throw new RuntimeException("termCount mismatch " + termCount + " vs " + (status.termCount - termCountStart));
+ if (termCount != -1 && termCount != status.delTermCount + status.termCount - termCountStart) {
+ throw new RuntimeException("termCount mismatch " + (status.delTermCount + termCount) + " vs " + (status.termCount - termCountStart));
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCheckIndex.java b/lucene/core/src/test/org/apache/lucene/index/TestCheckIndex.java
index c450fbc6998..cd33794142d 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestCheckIndex.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestCheckIndex.java
@@ -75,8 +75,8 @@ public class TestCheckIndex extends LuceneTestCase {
assertNotNull(seg.termIndexStatus);
assertNull(seg.termIndexStatus.error);
- assertEquals(19, seg.termIndexStatus.termCount);
- assertEquals(19, seg.termIndexStatus.totFreq);
+ assertEquals(18, seg.termIndexStatus.termCount);
+ assertEquals(18, seg.termIndexStatus.totFreq);
assertEquals(18, seg.termIndexStatus.totPos);
assertNotNull(seg.storedFieldStatus);