Distance values between the current point and each restaurant
Now, consider the scenario where you need to find the distance between the user location and each restaurant. How can we achieve this requirement? We can use scripts; the current geo coordinates are passed to the script and then the query to find the distance between each restaurant is run, as in the following code. Here, the current location is given as (1, 2)
:
curl -XPOST 'http://localhost:9200/restaurants/_search?pretty' -d '{ "script_fields": { "distance": { "script": "doc['"'"'location'"'"'].arcDistanceInKm(1, 2)" } }, "fields": [ "name" ], "query": { "match": { "name": "chinese" } } }'
We have used the function called arcDistanceInKm
in the preceding query, which accepts the geo coordinates and then returns the distance between that point and the locations satisfied by the query. Note that the unit of distance calculated is in kilometers (km). You might have noticed a long list of...