문제

I have the following Python code to query an ElasticSearch index. No matter what query I try, I get an empty resultset. I seem to be missing some basic trick here.

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
도움이 되었습니까?

해결책

Indeed, it is simple. The value of a term query should be lowercase. The following works fine.

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

I got the idea for this in this excellent tutorial.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top