Skip to content

Instantly share code, notes, and snippets.

@darklow
Created April 24, 2012 19:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darklow/2483009 to your computer and use it in GitHub Desktop.
Save darklow/2483009 to your computer and use it in GitHub Desktop.
Elasticsearch geo search debug
// Create index with mapping
curl -XPUT http://192.168.0.10:9200/geotest/ -d '{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"places" : {
"properties" : {
"name" : { "type" : "string" },
"location" : { "type" : "geo_point", "lat_lon": true }
}
}
}
}'
// Insert test data
curl -XPUT http://192.168.0.10:9200/geotest/places/1 -d '{
"name" : "closest",
"location": "56.958344835,24.110908139"
}'
curl -XPUT http://192.168.0.10:9200/geotest/places/2 -d '{
"name" : "average",
"location": "56.9624382022,24.1378033877"
}'
curl -XPUT http://192.168.0.10:9200/geotest/places/3 -d '{
"name" : "far",
"location": "56.9440017845,24.117343883"
}'
curl -XPUT http://192.168.0.10:9200/geotest/places/4 -d '{
"name" : "farthest",
"location": "56.9428606076,24.077425299"
}'
// Do search
curl -XGET http://192.168.0.10:9200/geotest/places/_search?pretty=true -d '{
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "3km",
"distance_type": "arc",
"location": {
"lat": 56.959942,
"lon": 24.113656
}
}
},
"sort": [
{
"_geo_distance": {
"location": [
56.959942,
24.113656
],
"order": "asc",
"unit": "km"
}
}
],
"fields": [
"_source"
],
"script_fields": {
"distance": {
"params": {
"lat": 56.959942,
"lon": 24.113656
},
"script": "doc[\u0027location\u0027].arcDistanceInKm(lat, lon)"
}
}
}'
How is it possible that sorting by _geo_distance ASC results shows in following order:
Pos Name Location Distance
------|---------------|-------------------------------|-------------------
1 far 56.9440017845,24.117343883 1.7865229796151898
2 average 56.9624382022,24.1378033877 1.4899987260417218
3 closest 56.958344835,24.110908139 0.243505338088285
4 farthest 56.9428606076,24.077425299 2.9042299224497112
----------------
// Raw output
----------------
{ "hits" : { "hits" : [ { "_id" : "3",
"_index" : "geotest",
"_score" : null,
"_source" : { "location" : "56.9440017845,24.117343883",
"name" : "far"
},
"_type" : "places",
"fields" : { "distance" : 1.7865229796151898 },
"sort" : [ 4500.475587902285 ]
},
{ "_id" : "2",
"_index" : "geotest",
"_score" : null,
"_source" : { "location" : "56.9624382022,24.1378033877",
"name" : "average"
},
"_type" : "places",
"fields" : { "distance" : 1.4899987260417218 },
"sort" : [ 4500.856454735725 ]
},
{ "_id" : "1",
"_index" : "geotest",
"_score" : null,
"_source" : { "location" : "56.958344835,24.110908139",
"name" : "closest"
},
"_type" : "places",
"fields" : { "distance" : 0.243505338088285 },
"sort" : [ 4501.804913492201 ]
},
{ "_id" : "4",
"_index" : "geotest",
"_score" : null,
"_source" : { "location" : "56.9428606076,24.077425299",
"name" : "farthest"
},
"_type" : "places",
"fields" : { "distance" : 2.9042299224497112 },
"sort" : [ 4502.240657295593 ]
}
],
"max_score" : null,
"total" : 4
} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment