How to reopen indexes during Lucene search

Source: Internet
Author: User
Document directory
  • 1. searcher construction method 1
  • 2. searcher construction method 2
How to reopen indexes during Lucene search

@ 2010-8-30 for & ever

 

Indexreader is a thread-safe object, which corresponds to the index directory one by one. instantiating indexreader is very resource-consuming. Generally, you only need to instantiate one indexreader for the same index directory during search.
When the index data is large, index data is usually hashed in multiple file directories (such as indexdir0, indexdir01, indexdir02) according to certain rules ).

When the index directory has an incremental update, you can use Lucene's reopen method to load the changed index fragments, instead of re-loading the complete index to save resources.

So how to use reopen?

 

1. searcher construction method 1

Send a file path string or directory to searcher. searcher maintains an internal reader. When this search is complete, this internal reader is disabled.

2. searcher construction method 2

After the reader is passed to the searcher and the searcher is constructed, the reader will not be turned off after the search ends, unless reader. Close () is called.

Therefore, you must use reader to construct searcher, and then use searcher. getindexreader () to obtain the reader of the current searcher,
Use reader again. iscurrent () determines whether the index file has changed. If the index file has changed, first disable the current searcher and then use reader. reopen () to obtain the new reader, and then create a new searcher.

If the searcher is obtained through the method of constructor 1 mentioned above, reopen cannot be used; otherwise, an exception that reader has disabled will be reported.

Code:
Indexreader reader = indexreader .().....
Indexsearcher = new indexsearcher (Reader); // note the indexsearcher construction method.
...
...
...
Indexreader reader1 = indexsearcher. getindexreader ();
If (! Reader1.iscurrent ()){
Indexsearcher. Close ();
Indexsearcher = new indexsearcher (reader1.reopen ());
}

Lucene API description:

---------------------------------------------------------
A. Search close Method
Note that the underlying indexreader is not closed, if indexsearcher was constructed with indexsearcher (indexreader R). If the indexreader was supplied implicitly by specifying a directory, then the indexreader gets closed

---------------------------------------------------------
B. Reopen method of indexreader
Refreshes an indexreader if the index has changed since this instance was (re) opened.
Opening an indexreader is an expensive operation. this method can be used to refresh an existing indexreader to reduce these costs. this method tries to only load segments that have changed or were created after the indexreader was (re) opened.

If the index has not changed since this instance was (re) opened, then this call is a Noop and returns this instance. otherwise, a new instance is returned. the old instance is not closed and remains usable.
Note: The re-opened reader instance and the old instance might share the same resources. for this reason no index modification operations (E. g. deletedocument (INT), setnorm (INT, String, byte) shocould be stored med using one of the readers until the old reader instance is closed. otherwise, the behavior of the readers is undefined.

You can determine whether a reader was actually reopened by comparing the old instance with the instance returned by this method:

Indexreader reader =...
...
Indexreader new = R. Reopen ();
If (New! = Reader ){
... // Reader was reopened
Reader. Close ();
}
Reader = new;

 

Forandever @ 2010-8-30

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.