0

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 Answer 1

2

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
                    }
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for answering. can you tell me from where to read elasticsearch from scratch
The official documentation would be a good place to start: elastic.co/guide/en/elasticsearch/reference/current/index.html
How to define GeoPointField() in elasticsearch django. It shows a serialization error. i am using library "django_elasticsearch_dsl"
Why did you change your question? My answer doesn't make any sense now. I've reverted your changes.
can you please tell me the answer if you know
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.