elasticsearch,pyes,Python,elasticsearch,Pyes" /> elasticsearch,pyes,Python,elasticsearch,Pyes" />

Python 如何以pyES提交查询?

Python 如何以pyES提交查询?,python,elasticsearch,pyes,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(do

我有以下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

事实上,这很简单。术语查询的值应为小写。下面的操作很好

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

我在这里得到了这个想法。

更准确地说,术语查询是一个过滤器,而不是一个实际的查询。过滤器不会通过分析器,因为它们会查找精确匹配。我想我打开了一个简单的小写字母分析器。