简体   繁体   English

索引和搜索时,Lucene是否需要相同的Analyzer实例?

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

I'm creating a dictionary app in Android with Lucene. 我正在使用Lucene在Android中创建字典应用程序。 Do I need to supply the same instance of StandardAnalyzer when indexing and searching, or can I just supply a new instance for both? 索引和搜索时是否需要提供相同的StandardAnalyzer实例,还是可以只为两者提供一个新实例?

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) ? 还是可以仅用new StandardAnalyzer(Version.LUCENE_36)替换analyzer所有用法? 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.). 唯一的要求是确保它们在搜索和索引编制期间的行为完全相同 (例如,应使用相同的对象构造函数,具有相同级别的数据访问权限,等等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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