elasticsearch - 获取正确的查询字符串

时间:2018-05-29 08:43:20

标签: python elasticsearch django-1.9

我是elasticsearch的新手,目前正在查询query_string结果的距离查询:这是我的代码:

doc = {
        "query": {
            "query_string": {
                "query": term,
                "fields": ['name', 'business_name', 'email', 'city',
                   'state', 'zip_code', 'business_keywords', 'phone_number',
                   'address', 'country'
                ], 
            }, 
            "filter": {
                "geo_distance": {
                    "distance": radius, 
                    "distance_unit": "km", 
                    "distance_type": "arc", 
                    "location": {
                        "lat": latitude, 
                        "lon": longitude
                    }
                }
            }
        }
    }
search_response = client.search(index="b",body=doc)

但是我收到以下错误:

RequestError:TransportError(400,u'search_phase_execution_exception',你无法解析搜索源。预期字段名称但得到[START_OBJECT]') 有什么想法??

1 个答案:

答案 0 :(得分:2)

您需要将query_stringgeo_distance个查询合并到一个bool查询中。

doc = {
        "query": {
          "bool": {
            "must": {
              "query_string": {
                "query": term,
                "fields": ['name', 'business_name', 'email', 'city',
                   'state', 'zip_code', 'business_keywords', 'phone_number',
                   'address', 'country'
                ], 
              }
            }, 
            "filter": {
                "geo_distance": {
                    "distance": radius, 
                    "distance_unit": "km", 
                    "distance_type": "arc", 
                    "location": {
                        "lat": latitude, 
                        "lon": longitude
                    }
                }
            }
        }
    }