Closing index pointer

I have a line in my Lucene code:

try
{
    searcher.GetIndexReader();
}
catch(Exception ex)
{
    throw ex;
}
finally
{
    if (searcher != null)
    {
        searcher.Close();
    }
}

      

In my finally clause, when I execute findcher.Close (), it will search for .GetIndexReader () as well. Close behind the scenes?

Or do I need to explicitly call the searcher.GetIndexReader () method. Close () to close IndexReader ??

Thanks for reading.

0


a source to share


2 answers


Sorry, it is difficult to understand what the search engine is in your snippet and how it was created. But you should not close the index reader with the Crawler .GetIndexReader (). Close () . The .Close () crawler will close all associated resources and the reader with an index, as well as an IF Crawler NOT NOT Index IndexSearcher built with IndexSearcher (IndexReader r) . In this case, you need to close the index reader manually.



+2


a source


First of all, code like this is always a bad idea:

try {
    // Do something
} catch(Exception ex) {
    throw ex;  // WRONG
}

      

You just mask the source of the exception and still throw. Better to just delete these lines.

If you didn't create it IndexReader

yourself, you don't need to close it yourself. Chances are high that you don't need to use the method at all getIndexReader

.



Also, if you don't assign searcher

in a block try

, there is no need to check if it is null as there is no way to get a null value.

Here's an example of what your code looks like:

String myIndexDir = "/my/index/dir";

IndexSearcher searcher = new IndexSearcher(myIndexDir);
try { 
    // Do something with the searcher
} finally { 
    searcher.close(); 
}

      

+1


a source







All Articles