# Search geo\_point Location by latitude and longitude - Java Query

**URL:** https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004
**Category:** Elasticsearch
**Created:** [June 7, 2012, 11:59am UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004 "2012-06-07T11:59:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Yehia](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@Ahmed\_Yehia](https://discuss.elastic.co/u/Ahmed_Yehia)
#### Post date: [June 7, 2012, 11:59am UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004/1 "2012-06-07T11:59:59Z")

</div>

Trying a Java query to search Locations by latitude/longitude pair:  
(Locations is indexed as geo\_point, with lat\_lon set to true)

SearchResponse response =  
client.prepareSearch("index1").setTypes("pin").setQuery((filteredQuery(matchAllQuery(),  
geoDistanceFilter("location").distance("1km").point(latitude,  
longitude)))).execute().actionGet();

This query works Ok, however it requires specifying a "distance" in the  
filter, Locations in my index should be unique,  
and so I 'm looking into another option to construct a query given only  
latitude & longitude as inputs, and the result being one or none.

I was wondering if specifying a distance may yields more than a single  
point.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 10, 2012, 9:35pm UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004/2 "2012-06-10T21:35:00Z")

</div>

You mean you want to check for exact match?

On Thu, Jun 7, 2012 at 1:59 PM, Ahmed Yehia [ahmed.yehia.m@gmail.com](mailto:ahmed.yehia.m@gmail.com) wrote:

> Trying a Java query to search Locations by latitude/longitude pair:  
> (Locations is indexed as geo\_point, with lat\_lon set to true)
> 
> SearchResponse response =  
> client.prepareSearch("index1").setTypes("pin").setQuery((filteredQuery(matchAllQuery(),  
> geoDistanceFilter("location").distance("1km").point(latitude,  
> longitude)))).execute().actionGet();
> 
> This query works Ok, however it requires specifying a "distance" in the  
> filter, Locations in my index should be unique,  
> and so I 'm looking into another option to construct a query given only  
> latitude & longitude as inputs, and the result being one or none.
> 
> I was wondering if specifying a distance may yields more than a single  
> point.

---

<div class="post-metadata">

### Author: ![Ahmed\_Yehia](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@Ahmed\_Yehia](https://discuss.elastic.co/u/Ahmed_Yehia)
#### Post date: [June 10, 2012, 10:12pm UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004/3 "2012-06-10T22:12:23Z")

</div>

Yes, this should be the case.  
Though I 'd be Ok with specifying an arbitrary distance if it has no effect  
in this context.

On Sunday, June 10, 2012, Shay Banon wrote:

> You mean you want to check for exact match?
> 
> On Thu, Jun 7, 2012 at 1:59 PM, Ahmed Yehia \<[ahmed.yehia.m@gmail.com](mailto:ahmed.yehia.m@gmail.com)\<javascript:\_e({}, 'cvml', 'ahmed.yehia.m@gmail.com');\>
> 
> > wrote:
> 
> > Trying a Java query to search Locations by latitude/longitude pair:  
> > (Locations is indexed as geo\_point, with lat\_lon set to true)
> > 
> > SearchResponse response =  
> > client.prepareSearch("index1").setTypes("pin").setQuery((filteredQuery(matchAllQuery(),  
> > geoDistanceFilter("location").distance("1km").point(latitude,  
> > longitude)))).execute().actionGet();
> > 
> > This query works Ok, however it requires specifying a "distance" in the  
> > filter, Locations in my index should be unique,  
> > and so I 'm looking into another option to construct a query given only  
> > latitude & longitude as inputs, and the result being one or none.
> > 
> > I was wondering if specifying a distance may yields more than a single  
> > point.

--

- Ahmed

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 13, 2012, 5:56pm UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004/4 "2012-06-13T17:56:38Z")

</div>

For exact match, you can have the lat and lon indexed (see the geo\_type  
mapping for it) and execute a query on both.

On Mon, Jun 11, 2012 at 12:12 AM, Ahmed Yehia [ahmed.yehia.m@gmail.com](mailto:ahmed.yehia.m@gmail.com)wrote:

> Yes, this should be the case.  
> Though I 'd be Ok with specifying an arbitrary distance if it has no  
> effect in this context.
> 
> On Sunday, June 10, 2012, Shay Banon wrote:
> 
> > You mean you want to check for exact match?
> > 
> > On Thu, Jun 7, 2012 at 1:59 PM, Ahmed Yehia [ahmed.yehia.m@gmail.com](mailto:ahmed.yehia.m@gmail.com)wrote:
> > 
> > > Trying a Java query to search Locations by latitude/longitude pair:  
> > > (Locations is indexed as geo\_point, with lat\_lon set to true)
> > > 
> > > SearchResponse response =  
> > > client.prepareSearch("index1").setTypes("pin").setQuery((filteredQuery(matchAllQuery(),  
> > > geoDistanceFilter("location").distance("1km").point(latitude,  
> > > longitude)))).execute().actionGet();
> > > 
> > > This query works Ok, however it requires specifying a "distance" in the  
> > > filter, Locations in my index should be unique,  
> > > and so I 'm looking into another option to construct a query given only  
> > > latitude & longitude as inputs, and the result being one or none.
> > > 
> > > I was wondering if specifying a distance may yields more than a single  
> > > point.
> 
> --
> 
> - Ahmed

---

<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, 3:24am UTC](https://discuss.elastic.co/t/search-geo-point-location-by-latitude-and-longitude-java-query/8004/5 "2017-07-06T03:24:33Z")

</div>


