#Use Case When we need to get only near geo points, no way to filter or get some small amount of geo points in query. Imagine when you have 500 geo points in single document, how would you identify or limit near or far locations?
See https://discuss.elastic.co/t/retrieving-only-matching-locations/54590 and http://stackoverflow.com/questions/37919891/how-to-limit-returning-nested-objects-in-elasticsearch ##Create Index
curl -XPUT localhost:9200/geo
curl -XPUT /geo/test/_mapping -d '{
"properties": {
"locations": {
"type": "geo_point"
}
}
}'
curl -XPOST "localhost:9200/geo/test/1" -d '{
"locations": [
{
"lon": 27.09519515440661,
"lat": 38.478413567935185
},
{
"lon": 34.80236053466797,
"lat": 39.81952667236328
},
{
"lon": 39.83582532931803,
"lat": 40.96827796407719
},
{
"lon": 36.333538930028226,
"lat": 41.28705790254833
},
{
"lon": 30.37797194349696,
"lat": 40.77331286046352
},
{
"lon": 28.254863739013672,
"lat": 37.468238830566406
},
{
"lon": 34.62808663310784,
"lat": 36.80608547444202
},
{
"lon": 27.417057,
"lat": 38.61295
},
{
"lon": 30.023533189852515,
"lat": 39.39730937110715
},
{
"lon": 32.5051864924701,
"lat": 37.89531395220301
},
{
"lon": 32.508596011506825,
"lat": 37.8492941159215
},
{
"lon": 32.488811510405185,
"lat": 37.87698760572874
}
]
}'
curl -XPOST "localhost:9200/geo/test/2" -d '{
"locations": [
{
"lon": 32.51198884183816,
"lat": 37.90110180235243
},
{
"lon": 32.03915426223398,
"lat": 36.540804011837125
},
{
"lon": 30.750701764736277,
"lat": 36.890245468561396
},
{
"lon": 30.595000683221542,
"lat": 36.84250172907872
},
{
"lon": 29.130974405626603,
"lat": 36.67013612740797
},
{
"lon": 28.442826999999966,
"lat": 37.021167
},
{
"lon": 27.357373237609863,
"lat": 37.056478551035404
},
{
"lon": 29.059229208694887,
"lat": 37.81761128155548
},
{
"lon": 27.260725335147754,
"lat": 37.843887300205395
},
{
"lon": 27.138333320617676,
"lat": 38.3168448192717
}
]
}'
curl -XPOST "localhost:9200/geo/test/3" -d '{
"locations": [
{
"lon": 27.29519515440661,
"lat": 38.378413567935185
}
}'
curl -XPOST localhost:9200/geo/test/_search -d '{
"query": {
"bool": {
"must": [
{
"geo_distance": {
"distance": "100km",
"locations": {
"lon": 28.442826999999966,
"lat": 37.121167
}
}
}
]
}
},
"sort": {
"_geo_distance": {
"locations": {
"lon": 28.442826999999966,
"lat": 37.121167
},
"order": "asc",
"unit": "km"
}
}
}'
Move custom script to es scripts dir /etc/elasticsearch/scripts/sortedGeoPoints.groovy