简体   繁体   中英

Does Lucene need the same Analyzer instance when indexing and when searching?

I'm creating a dictionary app in Android with Lucene. Do I need to supply the same instance of StandardAnalyzer when indexing and searching, or can I just supply a new instance for both?

For example, when I'm about to create an index, I do this:

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriter writer = new IndexWriter(directory,
                    new IndexWriterConfig(Version.LUCENE_36, analyzer));

And then, when getting the best fragments of the search term in the top documents, I do this:

TokenStream ts = TokenSources.getAnyTokenStream(indexSearcher.getIndexReader(),
                    hits[i].doc, "definition", analyzer);

Or can I just replace every usage of analyzer with new StandardAnalyzer(Version.LUCENE_36) ? I'm asking this because my indexing and search tasks are in different classes and I'd like to keep a minimum number of objects I'm passing across instances.

You can definitely use different instances of the same analyzer/tokenizer.

The only requirement is to ensure they behave exactly the same way during searching and indexing (eg same object constructors should be used, have the same level of data access, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM