2015-09-17 113 views
0

我想使用lucene使用python。 我用StandardAnalyser索引和搜索都。它工作正常,但現在,我的要求改變,我需要使用KeywordAnalyser關鍵詞lucene python的分析器?

代碼StandardAnalyser:

#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 

上面的代碼使用StandardAnalyser。我想用KeywordAnalyser而不是StandardAnalyser

我在下面的代碼中更改了分析器。 以下代碼正在使用關鍵字分析器但搜索未執行。

代碼KeywordAnalyser:

#Importing packages 
import lucene 
lucene.initVM() 
from org.apache.lucene import analysis, document, index, queryparser, search, store, util 

#Initialize Parameters 
analyzer = analysis.core.KeywordAnalyser(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 

任何幫助嗎?

+1

好了...所以用它。它在'analysis.core'中。你有什麼特別的問題嗎? – femtoRgon

+0

@femtoRgon:謝謝。我知道KeywordAnalyser,我需要使用'analysis.core'。索引正在工作,但搜索**不**工作。爲了搜索,我使用了'queryparser'。我認爲'queryparser'是問題,但它仍然沒有拋出任何錯誤,但搜索不起作用。 – iNikkz

回答

1

我找到了我的問題的解決方案。我需要analysis.core。我無法使用queryparser進行搜索,因爲它主要適用於StandardAnalyser。做搜索上KeywordAnalyser,我需要使用index.Termsearch.TermQuery

搜索代碼:

term_parser = index.Term("fieldname", entity) 
query = search.TermQuery(term_parser) 
hits = isearcher.search(query, None, 10).scoreDocs