# Can I use geo polygon filter to retrieve hits based on polygon fields?

**URL:** <https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345>\
**Category:** Elasticsearch\
**Created:** [November 11, 2013, 9:05am UTC](https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345 "2013-11-11T09:05:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Panagiotis](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Panagiotis](https://discuss.elastic.co/u/Panagiotis)\
**Post date:** [November 11, 2013, 9:05am UTC](https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345/1 "2013-11-11T09:05:49Z")

</div>

In this example  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html), I  
see a query that will return hits of documents with points that are within  
an area defined by a polygon. Is it possible to query for documents with  
polygons that overlap with another polygon (e.g.  
[http://postgis.refractions.net/docs/images/st\_overlaps03.png](http://postgis.refractions.net/docs/images/st_overlaps03.png))?

Thank you,  
Panagiotis

--  
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:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 14, 2013, 10:29am UTC](https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345/2 "2013-11-14T10:29:54Z")

</div>

Hey,

this does not work with geo\_point types, but you can built it using the  
geo\_shape type. See

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--Alex

On Mon, Nov 11, 2013 at 10:05 AM, Panagiotis [liakospanagiotis@gmail.com](mailto:liakospanagiotis@gmail.com)wrote:

> In this example  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html), I  
> see a query that will return hits of documents with points that are within  
> an area defined by a polygon. Is it possible to query for documents with  
> polygons that overlap with another polygon (e.g.  
> [http://postgis.refractions.net/docs/images/st\_overlaps03.png](http://postgis.refractions.net/docs/images/st_overlaps03.png))?
> 
> Thank you,  
> Panagiotis
> 
> --  
> 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).

--  
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:** ![Panagiotis](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Panagiotis](https://discuss.elastic.co/u/Panagiotis)\
**Post date:** [November 14, 2013, 10:41am UTC](https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345/3 "2013-11-14T10:41:22Z")

</div>

Thank you Alexander,

I had managed to figure out on my own that I can use geo shapes to achieve  
querying for intersecting objects. Now, I am wondering how costly it would  
be to index geo spatial data with elasticsearch given the fact that I need  
high precision. I wonder if anyone could point me to some stats so I do not  
have to explore it on my own.

I include the steps that helped me find the answer to my original question  
in case someone else faces the same problem.  
The data don't make much sense, I used both points and shapes to explore  
their differences.

--Panagiotis

1. create index and mapping

curl -XPUT 'localhost:9200/locations\_index'

curl -XPUT '[http://localhost:9200/locations\_index/location/\_mapping](http://localhost:9200/locations_index/location/_mapping)' -d '  
{  
"location":{  
"properties":{  
"longId":{  
"type":"long",  
"store":"yes"  
},  
"id":{  
"include\_in\_all":"false",  
"index":"not\_analyzed",  
"type":"string",  
"store":"yes"  
},  
"pin":{  
"type":"geo\_point",  
"store":"yes"  
},  
"shape": {  
"type": "geo\_shape",  
"tree": "quadtree",  
"precision": "1m"  
}  
}  
}  
}'

1. populate index

curl -XPUT '[http://localhost:9200/locations\_index/location/rd\_1700](http://localhost:9200/locations_index/location/rd_1700)' -d '  
{  
"longId": 1700,  
"id": "id\_1700",  
"pin": {  
"lat": 40.740000000000002,  
"lon": 73.989999999999995  
},  
"shape" : {  
"type" : "polygon",  
"coordinates" : [  
[[50.0, 0.0], [51.0, 0.0], [51.0, 1.0], [50.0, 1.0], [50.0,  
0.0] ]  
]  
}  
}'

curl -XPUT '[http://localhost:9200/locations\_index/location/rd\_1701](http://localhost:9200/locations_index/location/rd_1701)' -d '  
{  
"longId": 1701,  
"id": "id\_1701",  
"pin": {  
"lat": 30.740000000000002,  
"lon": 65.989999999999995  
},  
"shape" : {  
"type" : "polygon",  
"coordinates" : [  
[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0],  
[100.0, 0.0] ]  
]  
}  
}  
}'

1. run a geo distance query (on geo\_points)

curl -XGET '[http://localhost:9200/locations\_index/location/\_search](http://localhost:9200/locations_index/location/_search)' -d '  
{  
"from": 0,  
"size": 10,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"geo\_distance": {  
"distance": "10km",  
"pin": {  
"lat": 40.74,  
"lon": 73.99  
}  
}  
}  
}  
}  
}'

1. run a geo polygon query (on geo\_points)

curl -XGET '[http://localhost:9200/locations\_index/location/\_search](http://localhost:9200/locations_index/location/_search)' -d '  
{  
"from": 0,  
"size": 10,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter" : {  
"geo\_polygon" : {  
"pin" : {  
"points" : [  
{"lat" : 45, "lon" : 70},  
{"lat" : 30, "lon" : 60},  
{"lat" : 20, "lon" : 90}  
]  
}  
}  
}  
}  
}  
}'

1. run a geo shape query (on geo\_shapes)

curl -XGET '[http://localhost:9200/locations\_index/location/\_search](http://localhost:9200/locations_index/location/_search)' -d '  
{  
"from": 0,  
"size": 10,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"geo\_shape": {  
"shape": {  
"shape": {  
"type" : "polygon",  
"coordinates" : [  
[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0],  
[100.0, 1.0], [100.0, 0.0] ]  
]  
}  
}  
}  
}  
}  
}  
}'

--  
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:** ![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, 2:07am UTC](https://discuss.elastic.co/t/can-i-use-geo-polygon-filter-to-retrieve-hits-based-on-polygon-fields/14345/4 "2017-07-06T02:07:05Z")

</div>


