# Conditional query for geo location lookup

**URL:** <https://discuss.elastic.co/t/conditional-query-for-geo-location-lookup/17497>\
**Category:** Elasticsearch\
**Created:** [May 14, 2014, 1:17am UTC](https://discuss.elastic.co/t/conditional-query-for-geo-location-lookup/17497 "2014-05-14T01:17:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh2012](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/utkarsh2012/32/1187_2.png) [@utkarsh2012](https://discuss.elastic.co/u/utkarsh2012)\
**Post date:** [May 14, 2014, 1:17am UTC](https://discuss.elastic.co/t/conditional-query-for-geo-location-lookup/17497/1 "2014-05-14T01:17:28Z")

</div>

I have a usecase where I have 2 types of locations (i.e. with geo\_point  
type):

1. Location 1: Has a lat/lon with say radius=90 miles (it will vary) and  
type=outgoing
2. Location 2: Has a lat/lon with no radius and type=incoming

Now, when a query comes in with: lat/lon and radius=20, I expect this to  
happen:

1. Simple geo lookup: If the input lat/lon is within 20miles of location 2,  
return location 2.
2. If input lat/lon is within 90 miles of Location 1, return location 1 too  
in the result. If you notice, I want input radius to be overwritten by the  
saved radius for a specific type of location.

This is what I have come up with using script:  
{  
"query": {  
"match\_all": {}  
},  
"filter": {  
"script": {  
"script": "!doc['geopoint'].empty && doc['coverage\_type'].value ==  
'outgoing' ? doc['geopoint'].distanceInMiles(37,-121) \<=  
doc['radius'].value : doc['geopoint'].distanceInMiles(37,-121) \<= 20"  
}  
}  
}  
Where "37,-121" is input lat/lon and 20 is the input radius.

What do you think?

--  
Thanks,  
-Utkarsh

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CADjjot-aRjyyuHCfvPa2BrSFG6%3DPhqAcG1AwoszQF3dazXBHxQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADjjot-aRjyyuHCfvPa2BrSFG6%3DPhqAcG1AwoszQF3dazXBHxQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![utkarsh2012](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/utkarsh2012/32/1187_2.png) [@utkarsh2012](https://discuss.elastic.co/u/utkarsh2012)\
**Post date:** [May 15, 2014, 4:14pm UTC](https://discuss.elastic.co/t/conditional-query-for-geo-location-lookup/17497/2 "2014-05-15T16:14:07Z")

</div>

Bumping this one up. Any advice on the query?

On Tue, May 13, 2014 at 6:17 PM, Utkarsh Sengar [utkarsh2012@gmail.com](mailto:utkarsh2012@gmail.com)wrote:

> I have a usecase where I have 2 types of locations (i.e. with geo\_point  
> type):
> 
> 1. Location 1: Has a lat/lon with say radius=90 miles (it will vary) and  
> type=outgoing
> 2. Location 2: Has a lat/lon with no radius and type=incoming
> 
> Now, when a query comes in with: lat/lon and radius=20, I expect this to  
> happen:
> 
> 1. Simple geo lookup: If the input lat/lon is within 20miles of location  
> 2, return location 2.
> 2. If input lat/lon is within 90 miles of Location 1, return location 1  
> too in the result. If you notice, I want input radius to be overwritten by  
> the saved radius for a specific type of location.
> 
> This is what I have come up with using script:  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "script": {  
> "script": "!doc['geopoint'].empty && doc['coverage\_type'].value ==  
> 'outgoing' ? doc['geopoint'].distanceInMiles(37,-121) \<=  
> doc['radius'].value : doc['geopoint'].distanceInMiles(37,-121) \<= 20"  
> }  
> }  
> }  
> Where "37,-121" is input lat/lon and 20 is the input radius.
> 
> What do you think?
> 
> --  
> Thanks,  
> -Utkarsh

--  
Thanks,  
-Utkarsh

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CADjjot9Qxaq2Zd%2BdsCcX3YjJ4%3DKhAQ\_fzv0BsUva5R-KWcSKOg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADjjot9Qxaq2Zd%2BdsCcX3YjJ4%3DKhAQ_fzv0BsUva5R-KWcSKOg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:29am UTC](https://discuss.elastic.co/t/conditional-query-for-geo-location-lookup/17497/3 "2017-07-06T01:29:06Z")

</div>


