In the use of javasne, enabling and disabling indexreader and indexwriter is time-consuming, especially for indexreader operations,
(Indexreader reads the index information on the disk into the memory)
Therefore, we generally create indexreader as a single instance to reduce consumption.
Let's look at the example below:
Private indexreader reader; Public indexsearcher getsearcher (directory) {try {If (reader = NULL) {reader = indexreader. open (directory);} else {indexreader TR = indexreader. openifchanged (Reader); If (TR! = NULL) {reader. close (); reader = TR;} return New indexsearcher (Reader);} catch (corruptindexexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} return NULL ;}
In this case, we will not close the reader and check whether the reader exists during use. Then we can see another function openifchanged.
Explanation:
If the index is opened and the reader changes, a new reader is returned. Otherwise, null is returned. Therefore, openifchanged is used in the preceding method.
To determine whether it is the original reader.