Index: src/test/org/apache/lucene/index/TestDeletionPolicy.java =================================================================== --- src/test/org/apache/lucene/index/TestDeletionPolicy.java (revision 769840) +++ src/test/org/apache/lucene/index/TestDeletionPolicy.java (working copy) @@ -702,7 +702,7 @@ assertEquals(1+3*(N+1), policy.numOnInit); if (!autoCommit) - assertEquals(2*(N+1), policy.numOnCommit); + assertEquals(3*(N+1), policy.numOnCommit); IndexSearcher searcher = new IndexSearcher(dir); ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; Index: src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- src/java/org/apache/lucene/index/IndexWriter.java (revision 769840) +++ src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -1445,13 +1445,27 @@ // against an index that's currently open for // searching. In this case we write the next // segments_N file with no segments: + boolean doCommit; try { segmentInfos.read(directory); segmentInfos.clear(); + doCommit = false; } catch (IOException e) { // Likely this means it's a fresh directory + doCommit = true; } - segmentInfos.commit(directory); + + if (autoCommit || doCommit) { + // Always commit if autoCommit=true, else only + // commit if there is no segments file in this dir + // already. + segmentInfos.commit(directory); + synced.addAll(segmentInfos.files(directory, true)); + } else { + // Record that we have a change (zero out all + // segments) pending: + changeCount++; + } } else { segmentInfos.read(directory);