3

what is the difference between these two codes? based on performance oriented and documentation oriented

using directory directly in IndexSearcher

Analyzer anal = new StandardAnalyzer(Version.LUCENE_30);
QueryParser parser = new QueryParser(Version.LUCENE_30, "", anal);
Query query = parser.parse(queryStr);
Searcher searcher = new IndexSearcher(NIOFSDirectory.open(new File(indexDir)));

using directory in IndexReader then opening searcher using that reader

Analyzer anal = new StandardAnalyzer(Version.LUCENE_30);
QueryParser parser = new QueryParser(Version.LUCENE_30, "", anal);
Query query = parser.parse(queryStr);
IndexReader ir = IndexReader.open(NIOFSDirectory.open(new File(indexDir)), false);
IndexSearcher searcherNew = new IndexSearcher(ir);
4

1 回答 1

7

IndexSearcher is a lightweight wrapper around an IndexReader. Even if you use the IndexSearcher constructor, an IndexReader will be open under the hoods, so you can expect the same performance from your two snippets.

Altough it can be convenient, it is a bad practice to open directly an IndexSearcher over a Directory. Besides, this constructor is deprecated since Lucene 3.5.

于 2012-05-10T12:36:31.943 回答