Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top