lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brendan Grainger <>
Subject Closing directory in LuceneTestCase causes stacktrace
Date Thu, 28 Jun 2012 23:06:22 GMT

I'm trying to brush up on some of the *cough* newer APIs (we've been using 2.9.2 up until
now). Anyway, I have the test below which a modified version of one of the tests in Lucene
In Action, but uses LuceneTestCase as a base class. 

public class MetaphoneReplacementAnaylyzerTest extends LuceneTestCase {
	public void testKoolKat() throws Exception {
		Analyzer analyzer = new MetaphoneReplacementAnalyzer();
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
		// Directory directory = new RAMDirectory();
		Directory directory = newDirectory();
		IndexWriter writer = new IndexWriter(directory, config);
		Document doc = new Document();
		doc.add(new Field("contents", "cool cat", Field.Store.YES, Field.Index.ANALYZED));
		writer.addDocument(doc, analyzer);
		IndexSearcher searcher = new IndexSearcher(;
		// IndexSearcher searcher = new IndexSearcher(, true));
		Query q = new QueryParser(Version.LUCENE_36, "contents", analyzer).parse("kool kat");
		TopDocs hits =, 1);
		assertEquals(1, hits.totalHits);
		int docID = hits.scoreDocs[0].doc;
		doc = searcher.doc(docID);
		assertEquals("cool cat", doc.get("contents"));
		directory.close(); // if I comment this out, I get an assertion failure about the directory
not being closed


My issue is that I get the following stack trace when I try to close the directory. I've tried
variations of closing the writer instead of calling commit etc, but I think I'm just missing
something fundamental here. Note that obviously if I change :

	Directory directory = newDirectory();

	Directory directory = new RAMDirectory()

everything works, but I'd love to know how to correctly close the writer, searcher etc.

java.lang.RuntimeException: MockDirectoryWrapper: cannot close: there are still open files:
{_0.tis=1, _0.frq=1, _0.fdx=1, _0.prx=1, _0.fdt=1}
	at com.kuripai.lucene.analysis.MetaphoneReplacementAnaylyzerTest.testKoolKat(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
Caused by: java.lang.RuntimeException: unclosed IndexInput: _0.prx
	at org.apache.lucene.index.SegmentCoreReaders.<init>(
	at org.apache.lucene.index.SegmentReader.get(
	at org.apache.lucene.index.SegmentReader.get(
	at org.apache.lucene.index.DirectoryReader.<init>(
	at org.apache.lucene.index.ReadOnlyDirectoryReader.<init>(
	at org.apache.lucene.index.DirectoryReader$1.doBody(
	at org.apache.lucene.index.SegmentInfos$
	at com.kuripai.lucene.analysis.MetaphoneReplacementAnaylyzerTest.testKoolKat(
	... 39 more

Any insight would be greatly appreciated.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message