Elasticsearch Geo Distance query

I've got a list of places which have their latitude and longitude
associated with them in the correct mapping of geo_point

I've also got a query successfully returning results based on geo distance
which looks like this:

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"from": 0,
"size": 500
}

So this currently returns results within 30miles of the latitude and
longitude provided. And this works fine.

I'm struggling with the next step, which I'm hoping someone can point me in
the right direction with.

Each place has a field called distance which is an integer. This is the
maximum distance a place is willing to travel to a client. So if the
distance is 20 (miles) but their latitude and longitude calculates as more
than 20miles they should be excluded from the results.

The results come back like this:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "places",
"_id": "AUtvK2OILrMWSKLclj9Z",
"_score": null,
"_source": {
"id": "1",
"name": "Chubby Company",
"summary": "",
"content": "",
"locations": [
{
"lat": 51.8200763,
"lon": 0.5264076
}
],
"address": [
{
"addr1": "xxxx",
"addr2": "",
"town": "MyTown",
"county": "Essex",
"postcode": "XX1 2XX",
"tel1": "01111 111111",
"tel2": "",
"email": null
}
],
"website": "",
"media": {
"logo": "",
"image": "",
"video": ""
},
"product_ids": [
"1",
"3",
"2"
]
},
"sort": [
0.031774582056958885
]
}
]
}
}

The sort object/result is distance in miles, so the result above is 0.03
miles from the client.

I'm trying to utilize this to check against the record using result to
exclude it from the results but this is where I'm falling down.

I've tried different combinations of this:

"script": {
"script": "doc['distance'].value < doc['sort'].value"
}

which combined with the query looks like this:

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"filtered": {
"filter": {
"script": {
"script": "doc['distance'].value < doc['sort'].value"
}
}
},
"from": 0,
"size": 500
}

But i get an error of:

SearchPhaseExecutionException[Failed to execute phase [query], all shards

failed ... Parse Failure [No parser for element [filtered]

Any advice would be great.

I've also posted this on SO
(Elasticsearch Geo Distance query - Stack Overflow),
but now received many views, so thought I would go to the correct place to
ask

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/1e5098bd-5d08-4c9a-9b8a-de891e182a72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

I can't edit my original post for some reason..

Above it doesn't show the "distance" field in the results, it is there, i
just made an error pasting (trimmed the product ids field)

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "places",
"_id": "AUtvK2OILrMWSKLclj9Z",
"_score": null,
"_source": {
"id": "1",
"name": "Chubby Company",
"summary": "",
"content": "",
"locations": [
{
"lat": 51.8200763,
"lon": 0.5264076
}
],
"address": [
{
"addr1": "xxxx",
"addr2": "",
"town": "MyTown",
"county": "Essex",
"postcode": "XX1 2XX",
"tel1": "01111 111111",
"tel2": "",
"email": null
}
],
"website": "",
"media": {
"logo": "",
"image": "",
"video": ""
},
"product_ids": [
"1",
"3",
"2"
],
"distance": "20"
},
"sort": [
0.031774582056958885
]
}
]
}
}

I've also tried

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
},
"script": {
"script": "_source.distance < sort.value"
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"from": 0,
"size": 500
}

But i can't seem to successfully reference the distance and sort results

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/3e85e7ef-d6ff-4b0d-80b4-c9c393eabc8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.