2016-01-20 112 views
-1

我有這個代碼片段:排序蟒蛇嗖導致

from whoosh import index, qparser 

def find(text, indexDir, _doc_schema): 
    parser = qparser.QueryParser("content", schema=_doc_schema, termclass=FuzzyTerm) 
    query = parser.parse(text) 
    index = index.open_dir(indexDir) 
    with index.searcher() as searcher: 
     for res in searcher.search(query, limit=limit, terms=True): 
      result = { 
        "classname": res["classname"], 
        "code": res["code"], 
      } 
      if highlights: 
        result["text"] = res.highlights("content") 
      print "Res ", result['text'] 
      print "Matched terms ", res.matched_terms() 
      yield result 

有沒有一種辦法,而搜索我matched_terms通過相關級別排序?所以,如果我在尋找'aaa',我希望術語'aaa'先來,但它們可能會在'aab'結果後出現。

+0

您想排列結果或僅比條件嗎? –

+0

'打印「匹配的術語」,排序(res.matched_terms())' –

+0

bigOTHER,是的,我想排序結果,我又壞了。 – Sht0p0r

回答

0

如果產生匹配方面與結果一起,你可以建立術語詞典到的結果:

yield result, res.matched_terms() 

然後建立詞典:

>>> d = {} 
>>> for result, terms in find(text, indexDir, _doc_schema): 
...  for term in terms: 
...   d.setdefault(term, []).append(result) 

然後,您可以通過迭代字典順序:

>>> for term in sorted(d.keys()): 
...  print key, result['classname'], result['code']