# Search point in polygon

**URL:** <https://discuss.elastic.co/t/search-point-in-polygon/10992>\
**Category:** Elasticsearch\
**Created:** [March 4, 2013, 11:16am UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992 "2013-03-04T11:16:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 11:16am UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/1 "2013-03-04T11:16:42Z")

</div>

Hi,

I am looking for a solution for searching a point (lat/lon) in a polygon.  
Currently I am able to find (indexed) polygons by specifying a bounding box  
where the index polygon is in (with option WITHIN). But I need the  
opposite. I indexed polygons in my documents. Now I want all documents back  
based on query/filter with just a point (lat/lon). In other words give me  
all the documents where a given point is withing the (indexed) polygons.

How can I do this with ES?

thx in advanced.

Maarten Janssen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 4, 2013, 11:33am UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/2 "2013-03-04T11:33:58Z")

</div>

You should probably use intersects. The point intersects with the polygon.  
As noted in my earlier post, within is disappearing from the API apparently.

Another trick is to convert the point into a circle polygon. I do this for  
faking radius search. BTW. I have a library for doing stuff like this on  
github: [GitHub - jillesvangurp/geotools](https://github.com/jillesvangurp/geotools)

Jilles

On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:

> Hi,
> 
> I am looking for a solution for searching a point (lat/lon) in a polygon.  
> Currently I am able to find (indexed) polygons by specifying a bounding box  
> where the index polygon is in (with option WITHIN). But I need the  
> opposite. I indexed polygons in my documents. Now I want all documents back  
> based on query/filter with just a point (lat/lon). In other words give me  
> all the documents where a given point is withing the (indexed) polygons.
> 
> How can I do this with ES?
> 
> thx in advanced.
> 
> Maarten Janssen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 12:03pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/3 "2013-03-04T12:03:29Z")

</div>

Hi Jilles,

Thx for your quick reply. I tried with intersects (and a point) without  
luck. Even if I defined a point on the polygon line it won't find it.  
I also was thinking about using a circle/rectangle but the problem than is  
what should be the size of the circle? I mean it could be that multiple  
polygones are part of the defined circle (or if my radius is to small I  
still wouldn't find one).

thx for the library reference that is useful.

M

Op maandag 4 maart 2013 12:33:58 UTC+1 schreef Jilles van Gurp het volgende:

> You should probably use intersects. The point intersects with the polygon.  
> As noted in my earlier post, within is disappearing from the API apparently.
> 
> Another trick is to convert the point into a circle polygon. I do this for  
> faking radius search. BTW. I have a library for doing stuff like this on  
> github: [GitHub - jillesvangurp/geotools](https://github.com/jillesvangurp/geotools)
> 
> Jilles
> 
> On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> 
> > Hi,
> > 
> > I am looking for a solution for searching a point (lat/lon) in a polygon.  
> > Currently I am able to find (indexed) polygons by specifying a bounding box  
> > where the index polygon is in (with option WITHIN). But I need the  
> > opposite. I indexed polygons in my documents. Now I want all documents back  
> > based on query/filter with just a point (lat/lon). In other words give me  
> > all the documents where a given point is withing the (indexed) polygons.
> > 
> > How can I do this with ES?
> > 
> > thx in advanced.
> > 
> > Maarten Janssen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 4, 2013, 1:27pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/4 "2013-03-04T13:27:31Z")

</div>

Right, one hack here is to just use a really small circle (1 meter or so,  
or even less). Polygon borders tend to be not very exact anyway and it an  
error margin of one meter should be manageable.

Jilles

On Monday, March 4, 2013 1:03:29 PM UTC+1, Maarten Janssen wrote:

> Hi Jilles,
> 
> Thx for your quick reply. I tried with intersects (and a point) without  
> luck. Even if I defined a point on the polygon line it won't find it.  
> I also was thinking about using a circle/rectangle but the problem than is  
> what should be the size of the circle? I mean it could be that multiple  
> polygones are part of the defined circle (or if my radius is to small I  
> still wouldn't find one).
> 
> thx for the library reference that is useful.
> 
> M
> 
> Op maandag 4 maart 2013 12:33:58 UTC+1 schreef Jilles van Gurp het  
> volgende:
> 
> > You should probably use intersects. The point intersects with the  
> > polygon. As noted in my earlier post, within is disappearing from the API  
> > apparently.
> > 
> > Another trick is to convert the point into a circle polygon. I do this  
> > for faking radius search. BTW. I have a library for doing stuff like this  
> > on github: [GitHub - jillesvangurp/geotools](https://github.com/jillesvangurp/geotools)
> > 
> > Jilles
> > 
> > On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> > 
> > > Hi,
> > > 
> > > I am looking for a solution for searching a point (lat/lon) in a  
> > > polygon. Currently I am able to find (indexed) polygons by specifying a  
> > > bounding box where the index polygon is in (with option WITHIN). But I need  
> > > the opposite. I indexed polygons in my documents. Now I want all documents  
> > > back based on query/filter with just a point (lat/lon). In other words give  
> > > me all the documents where a given point is withing the (indexed) polygons.
> > > 
> > > How can I do this with ES?
> > > 
> > > thx in advanced.
> > > 
> > > Maarten Janssen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 3:39pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/5 "2013-03-04T15:39:41Z")

</div>

Hi Jilles,

I don't think the small circle is a solution that will work (or I miss the  
point literally :)). First of all the circle shape is not (yet) supported I  
guess in geo shape filtering. Secondly I don't see how a small circle will  
work with WITHIN or INTERSECTS. The use case is: give me all the docs with  
a polygon where the given input (search) point is part of. I think its the  
opposite of the WITHIN because this will take the search shape as the shape  
WITHIN the indexed documents should occur...

thus currently supported: _indexed_ shape WITHIN _filtered_ shape. I am  
looking for: _filtered_ shape/point WITHIN _indexed_ shape.

The INTERSECTS looks only to shapes that are partly (overlap) covered..if I  
try to build a small rectangle that is completely part of a polygon it  
gives no results with INTERSECTS. See example below

INDEXED:  
{"shape":{"location":{"type":"Polygon","coordinates":[[[2.0,1.0],[2.0,2.0],[1.0,2.0],[1.0,1.0],[2.0,1.0]]]},"text":"This  
is my first test polygon"}}  
SEARCH:  
{  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"geo\_shape" : {  
"location" : {  
"relation" : "intersects",  
"shape" : {  
"type" : "Envelope",  
"coordinates" : [[ 1.5, 1.5], [1.6, 1.4] ]  
}  
},  
"\_name" : null  
}  
}  
}  
}

If I stretch the envelop to be out of the indexed polygon INTERSECTS is  
working. Thus intersects only works if its part and not be part of the  
indexed polygon

of course could be that your solution will work but I miss the point.

M

On Mon, Mar 4, 2013 at 2:27 PM, Jilles van Gurp [jillesvangurp@gmail.com](mailto:jillesvangurp@gmail.com)wrote:

> Right, one hack here is to just use a really small circle (1 meter or so,  
> or even less). Polygon borders tend to be not very exact anyway and it an  
> error margin of one meter should be manageable.
> 
> Jilles
> 
> On Monday, March 4, 2013 1:03:29 PM UTC+1, Maarten Janssen wrote:
> 
> > Hi Jilles,
> > 
> > Thx for your quick reply. I tried with intersects (and a point) without  
> > luck. Even if I defined a point on the polygon line it won't find it.  
> > I also was thinking about using a circle/rectangle but the problem than  
> > is what should be the size of the circle? I mean it could be that multiple  
> > polygones are part of the defined circle (or if my radius is to small I  
> > still wouldn't find one).
> > 
> > thx for the library reference that is useful.
> > 
> > M
> > 
> > Op maandag 4 maart 2013 12:33:58 UTC+1 schreef Jilles van Gurp het  
> > volgende:
> > 
> > > You should probably use intersects. The point intersects with the  
> > > polygon. As noted in my earlier post, within is disappearing from the API  
> > > apparently.
> > > 
> > > Another trick is to convert the point into a circle polygon. I do this  
> > > for faking radius search. BTW. I have a library for doing stuff like this  
> > > on github: [https://github.com/\*\*jillesvangurp/geotools](https://github.com/**jillesvangurp/geotools)[https://github.com/jillesvangurp/geotools](https://github.com/jillesvangurp/geotools)
> > > 
> > > Jilles
> > > 
> > > On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I am looking for a solution for searching a point (lat/lon) in a  
> > > > polygon. Currently I am able to find (indexed) polygons by specifying a  
> > > > bounding box where the index polygon is in (with option WITHIN). But I need  
> > > > the opposite. I indexed polygons in my documents. Now I want all documents  
> > > > back based on query/filter with just a point (lat/lon). In other words give  
> > > > me all the documents where a given point is withing the (indexed) polygons.
> > > > 
> > > > How can I do this with ES?
> > > > 
> > > > thx in advanced.
> > > > 
> > > > Maarten Janssen
> > > 
> > > --  
> > > 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).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Maarten Janssen  
CTO Infohubble

Email: [mjanssen@infohubble.com](mailto:mjanssen@infohubble.com)  
URL: [www.infohubble.com](http://www.infohubble.com)  
Mobile: +31620006790  
Card: eee.am/mjanssen

_Address:_

Infohubble BV  
Gebouw Sevilla  
Entrada 304  
1096 ED Amsterdam  
The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 4, 2013, 4:23pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/6 "2013-03-04T16:23:05Z")

</div>

Ah sorry, I should have been more clear.

I actually convert the circle into a polygon using my library. I have a  
feature in there to do that. So, basically you end up with a circle like  
polygon (you can specify the number of segments you want) and intersect  
works great when you are intersecting two polygons.

I agree it is a bit of a hack though. But it is a good way to work around  
some limitations. I originally intended to use this library to index  
geohashes. This was before I figured out that Elasticsearch had evolved to  
support polygons. So there's a lot of functionality in there to work with  
all sorts of shapes and geohashes.

Jilles

On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:

> Hi,
> 
> I am looking for a solution for searching a point (lat/lon) in a polygon.  
> Currently I am able to find (indexed) polygons by specifying a bounding box  
> where the index polygon is in (with option WITHIN). But I need the  
> opposite. I indexed polygons in my documents. Now I want all documents back  
> based on query/filter with just a point (lat/lon). In other words give me  
> all the documents where a given point is withing the (indexed) polygons.
> 
> How can I do this with ES?
> 
> thx in advanced.
> 
> Maarten Janssen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 4:37pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/7 "2013-03-04T16:37:42Z")

</div>

Ok thx but still I have doubts this will work for my use case. The real use  
case is that I have described cities, municipalities and so on in polygons.  
Based on a point (part of street/address) I want to search for the  
corresponding city, municipality and country. I think the circle approach  
wouldnt help me. There is a workaround by search for each polygons all  
points (that works) but the use case I want to implement is more easy for  
getting the information I need.

M

On Mon, Mar 4, 2013 at 5:23 PM, Jilles van Gurp [jillesvangurp@gmail.com](mailto:jillesvangurp@gmail.com)wrote:

> Ah sorry, I should have been more clear.
> 
> I actually convert the circle into a polygon using my library. I have a  
> feature in there to do that. So, basically you end up with a circle like  
> polygon (you can specify the number of segments you want) and intersect  
> works great when you are intersecting two polygons.
> 
> I agree it is a bit of a hack though. But it is a good way to work around  
> some limitations. I originally intended to use this library to index  
> geohashes. This was before I figured out that Elasticsearch had evolved to  
> support polygons. So there's a lot of functionality in there to work with  
> all sorts of shapes and geohashes.
> 
> Jilles
> 
> On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> 
> > Hi,
> > 
> > I am looking for a solution for searching a point (lat/lon) in a polygon.  
> > Currently I am able to find (indexed) polygons by specifying a bounding box  
> > where the index polygon is in (with option WITHIN). But I need the  
> > opposite. I indexed polygons in my documents. Now I want all documents back  
> > based on query/filter with just a point (lat/lon). In other words give me  
> > all the documents where a given point is withing the (indexed) polygons.
> > 
> > How can I do this with ES?
> > 
> > thx in advanced.
> > 
> > Maarten Janssen
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Maarten Janssen  
CTO Infohubble

Email: [mjanssen@infohubble.com](mailto:mjanssen@infohubble.com)  
URL: [www.infohubble.com](http://www.infohubble.com)  
Mobile: +31620006790  
Card: eee.am/mjanssen

_Address:_

Infohubble BV  
Gebouw Sevilla  
Entrada 304  
1096 ED Amsterdam  
The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 4, 2013, 4:50pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/8 "2013-03-04T16:50:09Z")

</div>

Ah, you are trying to do a reverse geo coder. That's actually very similar  
to what I'm doing. I'm about to index a lot of open street map data for  
Berlin in the next few days to test this in a bit more detail. Basically  
given a polygon for Berlin and Amsterdam and a coordinate, you basically  
look for polygons that intersect with the 1m radius circle for your  
coordinate. Assuming the coordinate is somewhere in berlin, that would mean  
berlin intersects with that circle and the result set would include that  
but not Amsterdam.

Jilles

On Monday, March 4, 2013 5:37:42 PM UTC+1, Maarten Janssen wrote:

> Ok thx but still I have doubts this will work for my use case. The real  
> use case is that I have described cities, municipalities and so on in  
> polygons. Based on a point (part of street/address) I want to search for  
> the corresponding city, municipality and country. I think the circle  
> approach wouldnt help me. There is a workaround by search for each polygons  
> all points (that works) but the use case I want to implement is more easy  
> for getting the information I need.
> 
> M
> 
> On Mon, Mar 4, 2013 at 5:23 PM, Jilles van Gurp \<[jilles...@gmail.com](mailto:jilles...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Ah sorry, I should have been more clear.
> > 
> > I actually convert the circle into a polygon using my library. I have a  
> > feature in there to do that. So, basically you end up with a circle like  
> > polygon (you can specify the number of segments you want) and intersect  
> > works great when you are intersecting two polygons.
> > 
> > I agree it is a bit of a hack though. But it is a good way to work around  
> > some limitations. I originally intended to use this library to index  
> > geohashes. This was before I figured out that Elasticsearch had evolved to  
> > support polygons. So there's a lot of functionality in there to work with  
> > all sorts of shapes and geohashes.
> > 
> > Jilles
> > 
> > On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> > 
> > > Hi,
> > > 
> > > I am looking for a solution for searching a point (lat/lon) in a  
> > > polygon. Currently I am able to find (indexed) polygons by specifying a  
> > > bounding box where the index polygon is in (with option WITHIN). But I need  
> > > the opposite. I indexed polygons in my documents. Now I want all documents  
> > > back based on query/filter with just a point (lat/lon). In other words give  
> > > me all the documents where a given point is withing the (indexed) polygons.
> > > 
> > > How can I do this with ES?
> > > 
> > > thx in advanced.
> > > 
> > > Maarten Janssen
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Maarten Janssen  
> CTO Infohubble
> 
> Email: [mjan...@infohubble.com](mailto:mjan...@infohubble.com) \<javascript:\>  
> URL: [www.infohubble.com](http://www.infohubble.com)  
> Mobile: +31620006790  
> Card: eee.am/mjanssen
> 
> _Address:_
> 
> Infohubble BV  
> Gebouw Sevilla  
> Entrada 304  
> 1096 ED Amsterdam  
> The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 4:59pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/9 "2013-03-04T16:59:00Z")

</div>

Than I am happy to get your solution 🙂

The thing is intersects works only (thats what I get out of my test  
results) when 2 polygons have overlap. If one polygon is completely in the  
other polygon it will not see this as an intersection/overlap.  
So when you draw a circle (as a polygon) and do an INTERSECTS with the  
polygon of Berlin the chances are really there the circle is completely  
within the Berlin polygon and INTERSECTS wont give you Berlin back because  
it doesn't hit a polygon boundary. It would be much cleaner if we just  
could say give me all the documents where a given point is part of the  
defined polygons.

M

On Mon, Mar 4, 2013 at 5:50 PM, Jilles van Gurp [jillesvangurp@gmail.com](mailto:jillesvangurp@gmail.com)wrote:

> Ah, you are trying to do a reverse geo coder. That's actually very similar  
> to what I'm doing. I'm about to index a lot of open street map data for  
> Berlin in the next few days to test this in a bit more detail. Basically  
> given a polygon for Berlin and Amsterdam and a coordinate, you basically  
> look for polygons that intersect with the 1m radius circle for your  
> coordinate. Assuming the coordinate is somewhere in berlin, that would mean  
> berlin intersects with that circle and the result set would include that  
> but not Amsterdam.
> 
> Jilles
> 
> On Monday, March 4, 2013 5:37:42 PM UTC+1, Maarten Janssen wrote:
> 
> > Ok thx but still I have doubts this will work for my use case. The real  
> > use case is that I have described cities, municipalities and so on in  
> > polygons. Based on a point (part of street/address) I want to search for  
> > the corresponding city, municipality and country. I think the circle  
> > approach wouldnt help me. There is a workaround by search for each polygons  
> > all points (that works) but the use case I want to implement is more easy  
> > for getting the information I need.
> > 
> > M
> > 
> > On Mon, Mar 4, 2013 at 5:23 PM, Jilles van Gurp [jilles...@gmail.com](mailto:jilles...@gmail.com)wrote:
> > 
> > > Ah sorry, I should have been more clear.
> > > 
> > > I actually convert the circle into a polygon using my library. I have a  
> > > feature in there to do that. So, basically you end up with a circle like  
> > > polygon (you can specify the number of segments you want) and intersect  
> > > works great when you are intersecting two polygons.
> > > 
> > > I agree it is a bit of a hack though. But it is a good way to work  
> > > around some limitations. I originally intended to use this library to index  
> > > geohashes. This was before I figured out that Elasticsearch had evolved to  
> > > support polygons. So there's a lot of functionality in there to work with  
> > > all sorts of shapes and geohashes.
> > > 
> > > Jilles
> > > 
> > > On Monday, March 4, 2013 12:16:42 PM UTC+1, Maarten Janssen wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I am looking for a solution for searching a point (lat/lon) in a  
> > > > polygon. Currently I am able to find (indexed) polygons by specifying a  
> > > > bounding box where the index polygon is in (with option WITHIN). But I need  
> > > > the opposite. I indexed polygons in my documents. Now I want all documents  
> > > > back based on query/filter with just a point (lat/lon). In other words give  
> > > > me all the documents where a given point is withing the (indexed) polygons.
> > > > 
> > > > How can I do this with ES?
> > > > 
> > > > thx in advanced.
> > > > 
> > > > Maarten Janssen
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit [https://groups.google.com/d/](https://groups.google.com/d/)\*\*  
> > > topic/elasticsearch/\*\*bDUQKTK0oe0/unsubscribe?hl=en-\*\*US[https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> > > .  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).  
> > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > .
> > 
> > --  
> > Maarten Janssen  
> > CTO Infohubble
> > 
> > Email: [mjan...@infohubble.com](mailto:mjan...@infohubble.com)  
> > URL: [www.infohubble.com](http://www.infohubble.com)  
> > Mobile: +31620006790  
> > Card: eee.am/mjanssen
> > 
> > _Address:_
> > 
> > Infohubble BV  
> > Gebouw Sevilla  
> > Entrada 304  
> > 1096 ED Amsterdam  
> > The Netherlands
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Maarten Janssen  
CTO Infohubble

Email: [mjanssen@infohubble.com](mailto:mjanssen@infohubble.com)  
URL: [www.infohubble.com](http://www.infohubble.com)  
Mobile: +31620006790  
Card: eee.am/mjanssen

_Address:_

Infohubble BV  
Gebouw Sevilla  
Entrada 304  
1096 ED Amsterdam  
The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 4, 2013, 5:33pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/10 "2013-03-04T17:33:35Z")

</div>

Hmm, I just tried this and it seems you are right. Very annoying. I guess  
was assuming too much about intersects.

I guess you could turn things around and use a really large circle that for  
sure includes all the polygons you care about and then do a filter on  
polygon containment after you get the results. My library offers a contains  
function. But that is far from ideal.

This sounds like it might be a bug though since technically a large feature  
should intersect with any small feature contained by it.

Jilles

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Maarten\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@Maarten\_Janssen](https://discuss.elastic.co/u/Maarten_Janssen)\
**Post date:** [March 4, 2013, 5:44pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/11 "2013-03-04T17:44:10Z")

</div>

Or even better a new ShapeRelation should be produced called CONTAINS or  
IS\_PART\_OF where you get do the other way around. In our case (I think we  
need the same approach) we could go top down and match first all the  
polygons, and then use indeed a contains method, but in my opnion it should  
be part of the spatial search capabilities.

On Mon, Mar 4, 2013 at 6:33 PM, Jilles van Gurp [jillesvangurp@gmail.com](mailto:jillesvangurp@gmail.com)wrote:

> Hmm, I just tried this and it seems you are right. Very annoying. I guess  
> was assuming too much about intersects.
> 
> I guess you could turn things around and use a really large circle that  
> for sure includes all the polygons you care about and then do a filter on  
> polygon containment after you get the results. My library offers a contains  
> function. But that is far from ideal.
> 
> This sounds like it might be a bug though since technically a large  
> feature should intersect with any small feature contained by it.
> 
> Jilles
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Maarten Janssen  
CTO Infohubble

Email: [mjanssen@infohubble.com](mailto:mjanssen@infohubble.com)  
URL: [www.infohubble.com](http://www.infohubble.com)  
Mobile: +31620006790  
Card: eee.am/mjanssen

_Address:_

Infohubble BV  
Gebouw Sevilla  
Entrada 304  
1096 ED Amsterdam  
The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [March 5, 2013, 1:17pm UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/12 "2013-03-05T13:17:01Z")

</div>

Actually, I was wrong again. I did make it work in the end.

If others want to replicate my experiment, I pasted a few sample queries  
and responses as well as the data and mapping I indexed here:

> **[sample es requests and responses with geo\_shape - Pastebin.com](https://pastebin.com/QpXvYK5B)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

The first query shows that a query with a circle of 1m radius returns  
Berlin and Mitte for a circle near Rosenthalerplatz. So, the 1m circle  
polygon intersects with both polygons that fully contain the circle polygon.

If I increase the radius to 300 meter it also finds the nearby pois. There  
is a problem with accuracy though. I noticed that 100 meter doesn't return  
any of the pois. even though I know that it should have returned a few. If  
I increase to 150 it returns the Prenzlauerberg polygon but still no POIs.  
At 157 still the same. At 158, I suddenly get 8 results.

I might be able to improve the accuracy by playing with the levels in the  
mapping.

So what might be happening is that you are running into this accuracy issue  
here as well. If you haven't already, upgrading to a post 0.90-beta1  
snapshot build might help you. That would get you the changes made a few  
days ago to the geo\_shape implementation.

Jilles

On Monday, March 4, 2013 6:44:10 PM UTC+1, Maarten Janssen wrote:

> Or even better a new ShapeRelation should be produced called CONTAINS or  
> IS\_PART\_OF where you get do the other way around. In our case (I think we  
> need the same approach) we could go top down and match first all the  
> polygons, and then use indeed a contains method, but in my opnion it should  
> be part of the spatial search capabilities.
> 
> On Mon, Mar 4, 2013 at 6:33 PM, Jilles van Gurp \<[jilles...@gmail.com](mailto:jilles...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hmm, I just tried this and it seems you are right. Very annoying. I guess  
> > was assuming too much about intersects.
> > 
> > I guess you could turn things around and use a really large circle that  
> > for sure includes all the polygons you care about and then do a filter on  
> > polygon containment after you get the results. My library offers a contains  
> > function. But that is far from ideal.
> > 
> > This sounds like it might be a bug though since technically a large  
> > feature should intersect with any small feature contained by it.
> > 
> > Jilles
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Maarten Janssen  
> CTO Infohubble
> 
> Email: [mjan...@infohubble.com](mailto:mjan...@infohubble.com) \<javascript:\>  
> URL: [www.infohubble.com](http://www.infohubble.com)  
> Mobile: +31620006790  
> Card: eee.am/mjanssen
> 
> _Address:_
> 
> Infohubble BV  
> Gebouw Sevilla  
> Entrada 304  
> 1096 ED Amsterdam  
> The Netherlands

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Peter\_Johnson\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_johnson_2/32/8494_2.png) [@Peter\_Johnson\_2](https://discuss.elastic.co/u/Peter_Johnson_2)\
**Post date:** [June 26, 2014, 11:19am UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/13 "2014-06-26T11:19:59Z")

</div>

Did you get this working in the end Maarten?

I have the same problem with the way 'intersects' works and Jilles's  
solution doesn't work for me; possibly due to the 'tree\_levels' accuracy  
for quad tree.

As a kind of workaround, I was thinking that you could draw 2 'envelope'  
geo\_shape intersection; one vertical and one horizontal.  
Using a boolean filter you could reduce your results to only the polygons  
which intersect BOTH intersections.

It would look something like this:

[https://lh3.googleusercontent.com/-NkNXyNjCLwQ/U6wBLrRIbNI/AAAAAAAAJkU/383LpZNwVzE/s1600/bbox.png](https://lh3.googleusercontent.com/-NkNXyNjCLwQ/U6wBLrRIbNI/AAAAAAAAJkU/383LpZNwVzE/s1600/bbox.png)

Here's an example of the query:

> <https://gist.github.com/missinglink/ce25f1f9f7554ff1010f>

Unfortunately I couldn't get this working either, do you think this may be  
a simpler workaround for the 'intersects' issue?

-P

On Tuesday, 5 March 2013 14:17:01 UTC+1, Jilles van Gurp wrote:

> Actually, I was wrong again. I did make it work in the end.
> 
> If others want to replicate my experiment, I pasted a few sample queries  
> and responses as well as the data and mapping I indexed here:  
> [sample es requests and responses with geo\_shape - Pastebin.com](http://pastebin.com/QpXvYK5B)
> 
> The first query shows that a query with a circle of 1m radius returns  
> Berlin and Mitte for a circle near Rosenthalerplatz. So, the 1m circle  
> polygon intersects with both polygons that fully contain the circle polygon.
> 
> If I increase the radius to 300 meter it also finds the nearby pois. There  
> is a problem with accuracy though. I noticed that 100 meter doesn't return  
> any of the pois. even though I know that it should have returned a few. If  
> I increase to 150 it returns the Prenzlauerberg polygon but still no POIs.  
> At 157 still the same. At 158, I suddenly get 8 results.
> 
> I might be able to improve the accuracy by playing with the levels in the  
> mapping.
> 
> So what might be happening is that you are running into this accuracy  
> issue here as well. If you haven't already, upgrading to a post 0.90-beta1  
> snapshot build might help you. That would get you the changes made a few  
> days ago to the geo\_shape implementation.
> 
> Jilles
> 
> On Monday, March 4, 2013 6:44:10 PM UTC+1, Maarten Janssen wrote:
> 
> > Or even better a new ShapeRelation should be produced called CONTAINS or  
> > IS\_PART\_OF where you get do the other way around. In our case (I think we  
> > need the same approach) we could go top down and match first all the  
> > polygons, and then use indeed a contains method, but in my opnion it should  
> > be part of the spatial search capabilities.
> > 
> > On Mon, Mar 4, 2013 at 6:33 PM, Jilles van Gurp [jilles...@gmail.com](mailto:jilles...@gmail.com)  
> > wrote:
> > 
> > > Hmm, I just tried this and it seems you are right. Very annoying. I  
> > > guess was assuming too much about intersects.
> > > 
> > > I guess you could turn things around and use a really large circle that  
> > > for sure includes all the polygons you care about and then do a filter on  
> > > polygon containment after you get the results. My library offers a contains  
> > > function. But that is far from ideal.
> > > 
> > > This sounds like it might be a bug though since technically a large  
> > > feature should intersect with any small feature contained by it.
> > > 
> > > Jilles
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/bDUQKTK0oe0/unsubscribe?hl=en-US)  
> > > .  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > Maarten Janssen  
> > CTO Infohubble
> > 
> > Email: [mjan...@infohubble.com](mailto:mjan...@infohubble.com)  
> > URL: [www.infohubble.com](http://www.infohubble.com)  
> > Mobile: +31620006790  
> > Card: eee.am/mjanssen
> > 
> > _Address:_
> > 
> > Infohubble BV  
> > Gebouw Sevilla  
> > Entrada 304  
> > 1096 ED Amsterdam  
> > The Netherlands

--  
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/efa8d361-fc4f-46fb-9e99-d40910caa395%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/efa8d361-fc4f-46fb-9e99-d40910caa395%40googlegroups.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:19am UTC](https://discuss.elastic.co/t/search-point-in-polygon/10992/14 "2017-07-06T01:19:32Z")

</div>


