python - ElasticSearch GeoDistance Query -
i using geodistance query in python this
{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "geo_distance": { "distance": "20miles", "location": { "lat": 51.512497, "lon": -0.052098 } } } } } } it working correctly. problem how give "distance" value within document. have field distance: 50 in index each record , want use value of distance in geodistance. tried "distance":doc['distance'].value not working.
the ordinary queries , filters not expecting scripts placed within them.
assuming have stored geo_point field name of location, want use script filter using doc['field_name'].distanceinmiles(lat, lon) (as opposed distance(lat, lon) because standard units meters as-of v1.0.0):
{ "filtered" : { "query" : { "match_all" : { } }, "filter" : { "script" : { "script" : "doc['location'].distanceinmiles(lat, lon) < doc['distance'].value", "params" : { "lat" : 51.512497, "lon" : -0.052098 } } } } } if running instance of elasticsearch using standard units of miles (pre-v1.0.0), can use plain distance function (or if units distance happened in now-standard meters as-of v1.0.0):
{ "filtered" : { "query" : { "match_all" : { } }, "filter" : { "script" : { "script" : "doc['location'].distance(lat, lon) < doc['distance'].value", "params" : { "lat" : 51.512497, "lon" : -0.052098 } } } } } note: supply values of lat , lon directly within script one-off execution, scripts compiled and cached, using parameters allows reuse , therefore faster execution after first use.
as noted in documentation, can cache result of filter adding "_cache" : true filter, result of filter not cached default.
Comments
Post a Comment