本文整理汇总了Python中lucene.IndexSearcher.explain方法的典型用法代码示例。如果您正苦于以下问题:Python IndexSearcher.explain方法的具体用法?Python IndexSearcher.explain怎么用?Python IndexSearcher.explain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lucene.IndexSearcher
的用法示例。
在下文中一共展示了IndexSearcher.explain方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_lucene_index
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import explain [as 别名]
def search_lucene_index(search_params, index_dir, index_metadata, records_per_page):
"""
Uses the query term provided to search the disease ontology lucene index
"""
results = []
index_dir = SimpleFSDirectory(File(index_dir))
analyzer = build_perfield_analyzer(index_metadata)
searcher = IndexSearcher(index_dir)
index_fields = index_metadata.keys()
# Since we are paging results we want to grab what page we are on
page = (int(search_params.get('page', 1))) - 1
# Doing something pretty hacky here since we are trying to move from 0-based to 1
# based indexing to match our pagingation display
offset = int(page) * records_per_page
# If we are executing an advanced search we will be building a BooleanQuery
# in parts as opposed to the one MultiFieldQueryParser when doing a basic
# serach
query = None
if search_params.get('adv_search') == "True":
query = build_advanced_search_query(search_params, search_params.get('operator'), analyzer)
else:
parser = MultiFieldQueryParser(Version.LUCENE_CURRENT, index_fields, analyzer)
query = MultiFieldQueryParser.parse(parser, process_query_param((search_params.get('q'))))
# Parse through our hits
hits = searcher.search(query, 10000)
total_hits = hits.totalHits
count = min(hits.totalHits - offset, records_per_page)
for i in xrange(0, count):
score_doc = hits.scoreDocs[offset+i]
doc = searcher.doc(score_doc.doc)
term_id = doc.get('term id')
name = doc.get('name')
explain = searcher.explain(query, score_doc.doc)
match_fields = get_field_matches( explain.toString(), index_fields )
results.append( (term_id, name, list(match_fields)) )
searcher.close()
return (results, total_hits)
示例2: testSimple
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import explain [as 别名]
def testSimple(self):
class SimpleSimilarity(PythonSimilarity):
def lengthNorm(_self, field, numTerms):
return 1.0
def queryNorm(_self, sumOfSquaredWeights):
return 1.0
def tf(_self, freq):
return freq
def sloppyFreq(_self, distance):
return 2.0
def idfTerms(_self, terms, searcher):
return 1.0
def idf(_self, docFreq, numDocs):
return 1.0
def coord(_self, overlap, maxOverlap):
return 1.0
def scorePayload(_self, docId, fieldName, start, end, payload,
offset, length):
return 1.0
self.indexSingleFieldDocs([Field("contents", "x", Field.Store.YES,
Field.Index.ANALYZED)])
searcher = IndexSearcher(self.directory)
searcher.setSimilarity(SimpleSimilarity())
query = TermQuery(Term("contents", "x"))
explanation = searcher.explain(query, 0)
print explanation
scoreDocs = searcher.search(query, 50).scoreDocs
self.assertEqual(1, len(scoreDocs))
self.assertEqual(scoreDocs[0].score, 1.0)
del searcher
示例3: main
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import explain [as 别名]
def main(cls, argv):
if len(argv) != 3:
print "Usage: Explainer <index dir> <query>"
else:
indexDir = argv[1]
queryExpression = argv[2]
directory = SimpleFSDirectory(indexDir)
query = QueryParser(Version.LUCENE_CURRENT, "contents",
SimpleAnalyzer()).parse(queryExpression)
print "Query:", queryExpression
searcher = IndexSearcher(directory)
scoreDocs = searcher.search(query, 50).scoreDocs
for scoreDoc in scoreDocs:
doc = searcher.doc(scoreDoc.doc)
explanation = searcher.explain(query, scoreDoc.doc)
print "----------"
print doc["title"].encode('utf-8')
print explanation