我想使用lucene使用python。 我使用标准分析器进行索引和搜索。它工作得很好,但是现在,我的需求改变了,我需要使用关键字分析器。在
标准分析仪代码:
#Importing packages
import lucene
lucene.initVM()
from org.apache.lucene import analysis, document, index, queryparser, search, store, util
#Initialize Parameters
analyzer = analysis.standard.StandardAnalyzer(util.Version.LUCENE_CURRENT)
config = index.IndexWriterConfig(util.Version.LUCENE_CURRENT, self.analyzer)
directory = store.FSDirectory.open(File(<path_where_to_index>))
iwriter = None
iwriter = index.IndexWriter(directory, config)
#Indexing Part
doc = document.Document()
doc.add(document.Field("fieldname", entity, document.Field.Store.YES, document.Field.Index.ANALYZED))
doc.add(document.Field("category", category, document.Field.Store.YES, document.Field.Index.ANALYZED))
iwriter.addDocument(doc)
iwriter.commit()
#Searching Part
ireader = index.IndexReader.open(directory)
isearcher = search.IndexSearcher(ireader)
parser = queryparser.classic.QueryParser(util.Version.LUCENE_CURRENT, "fieldname", self.analyzer)
query = parser.parse(entity)
hits = isearcher.search(query, None, 100).scoreDocs
print hits
for hit in hits:
hitDoc = isearcher.doc(hit.doc)
print hitDoc
以上代码使用标准分析仪。我想用关键字分析器代替标准分析器。在
我在下面的代码里换了分析仪。下面的代码正在使用关键字分析器,但未执行搜索。在
关键字分析器的代码:
^{pr2}$有什么帮助吗?在
我找到了问题的答案。在
要使用关键字分析器,我需要}
analysis.core
。我不能使用queryparser
进行搜索,因为它主要用于标准分析器。要在关键字分析器上进行搜索,我需要使用index.Term
和{搜索代码:
相关问题 更多 >
编程相关推荐