Elasticsearch geo distance query exclude water bodies - elasticsearch

I am working on Elasticsearch and I have a use case where I have to list apartments within 20 miles from me.
Here is the query I am using:
{
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "20mi",
"distance_type": "plane",
"location": {
"lon": -122.4194,
"lat": 37.7749
}
}
}
]
}
}
}
But this query does not exclude water bodies and other inaccessible geography. Does elasticsearch exclude them under the query or is there a specific way to do it?

Related

ElasticSearch exclude result from being filtered where a condition is met

I want to exclude some results from being filtered by geo_distance where a condition is met.
For example, I filter my results on geo distance but I want to include all results where status is abnormal and meets the match_phrase query (even if it is outside the geo_distance)
GET /drive/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"keywords": "wheels"
}
},
{
"match_phrase": {
"name": "car sale"
}
}
],
"filter": [
{
"term": {
"status": "normal"
}
},
{
"geo_distance": {
"distance": "0.09km",
"address.coordinate": {
"lat": -33.703082,
"lon": 18.981069
}
}
}
]
}
}
}
I've been reading the documentation and googling, but I think I might be going in the wrong direction.
If you can point me in the right direction, or explain to me what a better solution could be to do this Id be very grateful.
From the doc :
should
The clause (query) should appear in the matching document. If the bool
query is in a query context and has a must or filter clause then a
document will match the bool query even if none of the should queries
match. In this case these clauses are only used to influence the
score. If the bool query is in a filter context or has neither must or
filter then at least one of the should queries must match a document
for it to match the bool query. This behavior may be explicitly
controlled by setting the minimum_should_match parameter.
So in your case the geo condition is in "should" since it can be optional and the rest in "filter" which are mandatory : the status and match_phrase
Try this:
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "normal"
}
},
{
"match_phrase": {
"name": "car sale"
}
},
{
"match_phrase": {
"keywords": "wheels"
}
}
],
"should": [
{
"geo_distance": {
"distance": "0.09km",
"address.coordinate": {
"lat": -33.703082,
"lon": 18.981069
}
}
}
]
}
}
}

Elasticsearch geo distance query range

Hi for a project I am trying to return users from elastic search which are in a range of 2km to 4km away from the search user.
I use the below query
`
{
"size": 1000,
"from": 0,
"_source": "user_id",
"query":{
"bool":{
"must_not": {
"terms": {
"user_id": []
}
},
"filter":[
{
"geo_distance_range":{
"from":"2km",
"to": "4km",
"location":{
"lon":-122.4194,
"lat":37.7749
}
}
}
]
}
}
}`
This query is deleted in elastic search version 6.3 which is the version I am using.
Can anyone please help me solve this use case in elastic search 6.3? Aggregations only returns the number of users in the range but I want to return complete results of all users in the range.
I can't test this, but it seems reasonable that you should be able to combine must and must_not clauses with geo_distance:
"query": {
"bool": {
"must_not": {
"terms": {
"user_id": []
},
"geo_distance": {
"distance": "2km",
"location": [-122.4194, 37.7749]
}
},
"must": {
"geo_distance": {
"distance": "4km",
"location": [-122.4194, 37.7749]
}
}
}
}

geo_distance doesn't return any hit Elasticsearch

Have a problem with this query, when I use geo_distance filter, nothing returned from query. When I remove it I get proper results. Query is bellow:
GET _search
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": 20,
"distance_unit": "km",
"coordinates": [48.8488576, 2.3354223]
}
},
"must": {
"term": {
"_type": {
"value": "staff"
}
}
},
"must_not": [
{
"term": {
"cabinet.zipcode": {
"value": "75006"
}
}
},
{
"term": {
"next_availability_in_days": {
"value": "-1"
}
}
}
]
}
}
}
I would appreciate if someone gives me a hint.
UPDATE
When I run Elasticsearch Ruby DSL with same query logic, I get proper results:
<Elasticsearch::Model::Searching::SearchRequest:0x007ff335763560
#definition=
{:index=>["development_app_scoped_index_20170428134744",
"development_app_scoped_index_20170428134744"], :type=>["staff", "light_staff"],
:body=>
{:query=>
{:bool=>
{:must_not=>[
{:term=>{"cabinet.zipcode"=>75006}},
{:term=> {:next_availability_in_days=>-1}}
],
:must=>[
{:term=>{:_type=>"staff"}}
],
:filter=>{:geo_distance=>
{:coordinates=>
{:lat=>48.8488576, :lon=>2.3354223},
:distance=>"6km"
}
}}},
:sort=>[
{:type=>{:order=>"desc"}},
{"_geo_distance"=>{"coordinates"=>"48.8488576,2.3354223", "order"=>"asc",
"unit"=>"km"}},
{:next_availability_in_days=>{:order=>"asc"}},
{:priority=>{:order=>"asc"}}
]
}}
So this is really weird and I'm not sure what's going wrong in ES syntax, but it definitely should work as expected.
Thanks.
There is probably nothing in the range that you have entered.
Try to increase the "distance": 20 field to "distance": 500 and check the results then. For example the distance between these two geo points [0,0] and [0,1] is ~138.3414KM .
Another suggestion is to get rid of the "distance_unit" field and put the
and put the KM inside the "distance" field as following:
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "20km",
"coordinates": [
48.8488576,
2.3354223
]
}
}
}
}
}

Elasticsearch apply condintions in query on basis of results count

Is there any way in Elasticsearch for following type of outcome
"Apply first condition, if no results found then apply next conditions and so on.."
I am aware of basics of ES queries. I know this can be done by querying again and again on results basis but I want to do this in single query for the sake of time and efficiency.
Here is my current query
GET_search{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 28.6143519,
"lon": -81.50773
},
"bottom_right": {
"lat": 28.3479859,
"lon": -81.22977
}
}
}
}
]
}
}
}
},
"size": 10,
"from": 0,
"sort": {
"search_score": {
"order": "desc"
}
}
}
Now what I want to do is, if this query hits zero results then this should search for another increased set of lat lon bounds. I can do this by requering elasticsearch but it will be an inefficient way.
I want to know if is this possible in elasticsearch?

Elasticsearch geo_distance filter - distance unit set to km but seems to calculate with meters

I'm using geo_distance filter to get the points that are within a certain distance to a reference point. As a distance_unit I set km, however, when I'm running my code I'm pretty sure that Elastic calculates meters, and not kilometers (so in this particular case below I get the points that are within 20 meters, not 20 kilometers).
Any ideas why is it using meters rather than kilometers?
{
"query": {
"filtered": {
"query": {
"bool": {
"must_not": [
{
"term": {
"_id": {
"value": idnum
}
}
}
]
}
},
"filter": {
"geo_distance": {
"distance": 20,
"distance_unit": "km",
"geopoint": {
"lat": lat,
"lon": lng
}
}
}
}
}
}
use "distance": "20KM", rather than distance_unit....

Resources