July 2015
Intermediate to advanced
192 pages
3h 38m
English
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 ...
Read now
Unlock full access