Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1525890) +++ lucene/CHANGES.txt (working copy) @@ -85,6 +85,10 @@ * LUCENE-4998: Fixed a few places to pass IOContext.READONCE instead of IOContext.READ (Shikhar Bhushan via Mike McCandless) +* LUCENE-5242: DirectoryTaxonomyWriter.replaceTaxonomy did not fully reset + its state, which could result in exceptions being thrown, as well as + incorrect ordinals returned from getParent. (Shai Erera) + API Changes: * LUCENE-5222: Add SortField.needsScores(). Previously it was not possible Index: lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (revision 1525890) +++ lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (working copy) @@ -981,6 +981,7 @@ initReaderManager(); // ensure that it's initialized refreshReaderManager(); nextID = indexWriter.maxDoc(); + taxoArrays = null; // must nullify so that it's re-computed next time it's needed // need to clear the cache, so that addCategory won't accidentally return // old categories that are in the cache. @@ -987,6 +988,7 @@ cache.clear(); cacheIsComplete = false; shouldFillCache = true; + cacheMisses.set(0); // update indexEpoch as a taxonomy replace is just like it has be recreated ++indexEpoch; Index: lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java =================================================================== --- lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java (revision 1525890) +++ lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java (working copy) @@ -469,4 +469,27 @@ IOUtils.close(indexDir, taxoDir); } + + @Test + public void testReplaceTaxoWithLargeTaxonomy() throws Exception { + Directory srcTaxoDir = newDirectory(), targetTaxoDir = newDirectory(); + + // build source, large, taxonomy + DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(srcTaxoDir); + int ord = taxoWriter.addCategory(new CategoryPath("A/1/1/1/1/1/1", '/')); + taxoWriter.close(); + + taxoWriter = new DirectoryTaxonomyWriter(targetTaxoDir); + int ordinal = taxoWriter.addCategory(new CategoryPath("B/1", '/')); + assertEquals(1, taxoWriter.getParent(ordinal)); // call getParent to initialize taxoArrays + taxoWriter.commit(); + + taxoWriter.replaceTaxonomy(srcTaxoDir); + assertEquals(ord - 1, taxoWriter.getParent(ord)); + taxoWriter.close(); + + srcTaxoDir.close(); + targetTaxoDir.close(); + } + }