elasticsearch -- getting right query string

813 Views Asked by At

i am new to elasticsearch and currently looking for distance query on results of query_string: Here is my code:

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)

but i am getting the following error :

RequestError: TransportError(400, u'search_phase_execution_exception', u'failed to parse search source. expected field name but got [START_OBJECT]') Any idea ??

1

There are 1 best solutions below

7
On BEST ANSWER

You need to combine your query_string and geo_distance queries into a bool query.

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
                    }
                }
            }
        }
    }