如何在pyES中提交查询?

时间:2014-04-22 14:25:36

标签: python elasticsearch pyes

我有以下Python代码来查询ElasticSearch索引。无论我尝试什么查询,我都得到一个空的结果集。我似乎在这里错过了一些基本技巧。

import sys
import pyes
from pyes.query import TermQuery, FuzzyLikeThisFieldQuery, BoolQuery


conn = pyes.ES(('http', '?????.qbox.io', '80'))
INDEX = "postoffice"

def state_query(doc):
    return TermQuery(field="STATE_ALPHA", value=doc["state"])

def fuzzy_county_query(doc):
    return FuzzyLikeThisFieldQuery(field="COUNTY_NAME", like_text=doc["county"])

def fuzzy_name_query(doc):
    return FuzzyLikeThisFieldQuery(field="FEATURE_NAME", like_text=doc["place"])

def find_within_county(doc):
    return BoolQuery(must=[state_query(doc), fuzzy_county_query(doc)], should=fuzzy_name_query(doc))

if __name__ == "__main__":
    test = dict(place="Rockport", county="Essex", state="MA")
    q = find_within_county(test)
    print q._serialize()
    results = conn.search(query=q, indices=[INDEX])
    for result in results:
        print result

1 个答案:

答案 0 :(得分:0)

确实,这很简单。术语查询的值应为小写。以下工作正常。

def state_query(doc):
    return TermQuery(field="STATE_ALPHA", value=doc["state"].lower())

我在这个excellent tutorial中得到了这个想法。