当前位置: 首页>>代码示例>>Python>>正文


Python IndexSearcher.explain方法代码示例

本文整理汇总了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)
开发者ID:bethune,项目名称:disease-ontology,代码行数:48,代码来源:do_lucene_search.py

示例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
开发者ID:qiugen,项目名称:pylucene-trunk,代码行数:45,代码来源:ScoreTest.py

示例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
开发者ID:pombredanne,项目名称:python-lucenepp,代码行数:26,代码来源:Explainer.py


注:本文中的lucene.IndexSearcher.explain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。