-
-
Save alexeiemam/04eda6fff5915f9cef66 to your computer and use it in GitHub Desktop.
Elasticsearch Tests :: Access GeoDistance without distance sorting in object field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create index | |
curl -X POST http://localhost:9200/placeholders -d '{ | |
"mappings":{ | |
"placeholder":{ | |
"properties":{ | |
"name" : { "type" : "string" }, | |
"main_place" : { "type":"geo_point" }, | |
"all_places":{ | |
"type":"object", | |
"properties":{ | |
"location_point":{ "type":"geo_point" } | |
} | |
} | |
} | |
} | |
}, | |
"settings":{ | |
"number_of_shards":1, | |
"number_of_replicas":0 | |
} | |
}' | |
# Create document | |
curl -XPUT http://localhost:9200/placeholders/placeholder/1 -d '{ | |
"name": "an identifier", | |
"main_place": "56.958344835,24.110908139", | |
"all_places": [ | |
{ | |
"location_point": { | |
"lat": 56.958344835, | |
"lon": 24.110908139 | |
} | |
}, | |
{ | |
"location_point": { | |
"lat": 57, | |
"lon": 24 | |
} | |
} | |
] | |
}' | |
# 1. Sort by geodistance on all_places (max) | |
curl -XGET http://localhost:9200/placeholders/placeholder/_search?pretty=true -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"sort" : [ | |
{ | |
"_geo_distance" : { | |
"all_places.location_point" : {"lat":56.959942, "lon":24.113656}, | |
"order" : "asc", | |
"unit" : "km", | |
"mode" : "max" | |
} | |
} | |
] | |
}' | |
{......... | |
"sort" : [ 8.191661796545175 ] | |
.........} | |
# 2. Sort by geodistance on all_places (min) | |
curl -XGET http://localhost:9200/placeholders/placeholder/_search?pretty=true -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"sort" : [ | |
{ | |
"_geo_distance" : { | |
"all_places.location_point" : {"lat":56.959942, "lon":24.113656}, | |
"order" : "asc", | |
"unit" : "km", | |
"mode" : "min" | |
} | |
} | |
] | |
}' | |
{......... | |
"sort" : [ 0.24320564104938122 ] | |
.........} | |
# 3. Calculate distance on main_place | |
curl -XGET http://localhost:9200/placeholders/placeholder/_search?pretty=true -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"script_fields": { | |
"distance": { | |
"params": { | |
"lat": 56.959942, | |
"lon": 24.113656 | |
}, | |
"script": "doc.main_place.arcDistanceInKm(lat, lon)" | |
} | |
} | |
}' | |
{............ | |
"distance" : [ 0.2432056410493809 ] | |
............} | |
# 4. Calculate distance on first of all_places via script_fields (Attempt: 1, using ``doc``) | |
# (As documented, only simple types, not Objects, are available on doc) | |
curl -XGET http://localhost:9200/placeholders/placeholder/_search?pretty=true -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"script_fields": { | |
"distance": { | |
"params": { | |
"lat": 56.959942, | |
"lon": 24.113656 | |
}, | |
"script": "doc.all_places.location_point.arcDistanceInKm(lat, lon)" | |
} | |
} | |
}' | |
{ | |
"error" : "[...] PropertyAccessException[[Error: could not access: all_places; in class: org.elasticsearch.search.lookup.DocLookup]\n[Near : {... doc.all_places.location_point. ....}]\n ^\n[Line: 1, Column: 1]]}]", | |
"status" : 500 | |
} | |
# 5. Calculate distance on first of all_places via script_field (Attempt: 2 using ``_source``) | |
# (geopoint lat/lon data accessed via _source is a HashMap, not a GeoPoint) | |
curl -XGET http://localhost:9200/placeholders/placeholder/_search?pretty=true -d '{ | |
"query": { | |
"match_all": {} | |
}, | |
"script_fields": { | |
"distance": { | |
"params": { | |
"lat": 56.959942, | |
"lon": 24.113656 | |
}, | |
"script": "(_source.all_places.size() > 0) ? _source.all_places.get(0).location_point.arcDistanceInKm(lat,lon) : Double.POSITIVE_INFINITY " | |
} | |
} | |
}' | |
{ | |
"error" : "[...] PropertyAccessException[[Error: unable to resolve method: java.util.HashMap.arcDistanceInKm(java.lang.Double, java.lang.Double) [arglength=2]]\n[Near : {... (_source.all_places.size() > 0 ....}]\n ^\n[Line: 1, Column: 1]]}]", | |
"status" : 500 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment