Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Lucene: Java-User

How to safely close Index objects?

 

 

Lucene java-user RSS feed   Index | Next | Previous | View Threaded


jrhoden at unimelb

Nov 10, 2009, 9:13 PM

Post #1 of 2 (333 views)
Permalink
How to safely close Index objects?

Hi Guys,

Given a class with two static variables, is the following safe? ie If
I call "close" while something else is using the objects, do the
objects simply hold a flag saying they need to be destroyed once the
objects are finished being used, or do they not track if anything is
currently using the object and simply blow up if I try to close them?

public static void rebuildIndex() throws IOException, ParseException {

if(dir==null)
dir = new NIOFSDirectory(new
File(Configuration.getAttachmentFolder()), null);

IndexWriter w = new IndexWriter(dir, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
.... do the building here ...
w.optimize();
w.commit();
w.close();

// Remember the current search objects
IndexReader oldIndexReader = indexReader;
IndexSearcher oldIndexSearcher = searcher;

// Allocate new search objects
indexReader = IndexReader.open(dir,true);
searcher = new IndexSearcher(indexReader);

// Cleanup on the old search objects
try {
if(oldIndexSearcher!=null)
oldIndexSearcher.close();
if(oldIndexReader!=null)
oldIndexReader.close();
} catch(IOException e) {
// There's nothing we can do if a file was not able to be closed
correctly.
}
}

I am not sure if I need to create and remember about both the index
reader and index searcher, the javadocs are a bit unclear on that.

How does one get the early edition of the lucene book? I couldn't work
out how from the website. I am assuming I am missing something obvious
(:

Thanks,
Jacob

____________________________________
Information Technology Services,
The University of Melbourne

Email: jrhoden [at] unimelb
Phone: +61 3 8344 2884
Mobile: +61 4 1095 7575


lucene at mikemccandless

Nov 11, 2009, 1:56 AM

Post #2 of 2 (295 views)
Permalink
Re: How to safely close Index objects? [In reply to]

On Wed, Nov 11, 2009 at 12:13 AM, Jacob Rhoden <jrhoden [at] unimelb> wrote:

> Given a class with two static variables, is the following safe? ie If I call
> "close" while something else is using the objects, do the objects simply
> hold a flag saying they need to be destroyed once the objects are finished
> being used, or do they not track if anything is currently using the object
> and simply blow up if I try to close them?

You shouldn't close your IndexReader/IndexSearcher unless all searches
are done using them, otherwise you'll hit exceptions.

Also, if you don't separately use the IndexReader, you can just
open/close the IndexSearcher.

However, it's better to use IndexReader's reopen method, which only
opens the new segments created since the last time the reader was
opened.

> How does one get the early edition of the lucene book? I couldn't work out
> how from the website. I am assuming I am missing something obvious (:

You can get the book here http://www.manning.com/hatcher3 (NOTE: I'm
one of the authors!).

Chapter 11 in the book has a class called SearcherManager, that
handles the details of reopen/closing the IndexReader while queries
are still in flight, that might be useful here.

Mike

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe [at] lucene
For additional commands, e-mail: java-user-help [at] lucene

Lucene java-user RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.