tags:

views:

350

answers:

1

When the code executes below I assume that the stopwords file is read from the file system every time I parse a query. Instead of doing this, can I reuse the same instance of the analyzer instead of creating a new one? Is it thread-safe? (After much googling I can not find any information on this)

var stopwordsFile = new FileInfo("C:\MyStopWordsFile.txt");
var analyzer = new StandardAnalyzer(stopwordsFile);
var queryParser = new QueryParser("", analyzer);
var query = queryParser.Parse(stringToParse);
+2  A: 

The docs state that only static instances of StandardAnalyzer are thread safe. QueryParser is the same.

Si
Yes, I noticed that. It just seems strange to me that Lucene will then reload and parse the stopwords file from the file system for every query. Because of performance reasons, but also I guess there may be file system locking issues aswell when 2 threads reads the file at the same time. Iv also seen samples where I would interpret it that reusing the analyzer is possible, even though the note in the doc seem to contradict that.
Alex Johansson
@Alex, there will be no locking or other access issues if file is opened only for read and it's not exclusively locked.
zihotki
Yes, that is true, assuming Lucene does it that way. Since a note says the class is not thread safe, one might assume that that assumtion might be incorrect.
Alex Johansson