# Newbie question

**URL:** <https://discuss.elastic.co/t/newbie-question/9621>\
**Category:** Elasticsearch\
**Created:** [November 8, 2012, 6:37am UTC](https://discuss.elastic.co/t/newbie-question/9621 "2012-11-08T06:37:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_3](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@Ali\_3](https://discuss.elastic.co/u/Ali_3)\
**Post date:** [November 8, 2012, 6:37am UTC](https://discuss.elastic.co/t/newbie-question/9621/1 "2012-11-08T06:37:03Z")

</div>

Hello,  
Thanks in advance for looking at my question.  
I would like to store a collection of polygons (each point of the polygon  
being a lat, long position). Next, given a point (lat, long) say p I would  
like to know which polygons enclose/contain point p.  
Does elastic search support such a query? If not what is the closest thing  
elastic search can support?

Thanks,  
Ali

--

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [November 8, 2012, 9:50am UTC](https://discuss.elastic.co/t/newbie-question/9621/2 "2012-11-08T09:50:01Z")

</div>

hey,

take a look at  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-polygon-filter.html) maybe  
this is sufficient for your usecase?

simon

On Thursday, November 8, 2012 7:37:03 AM UTC+1, Ali wrote:

> Hello,  
> Thanks in advance for looking at my question.  
> I would like to store a collection of polygons (each point of the polygon  
> being a lat, long position). Next, given a point (lat, long) say p I would  
> like to know which polygons enclose/contain point p.  
> Does Elasticsearch support such a query? If not what is the closest  
> thing Elasticsearch can support?
> 
> Thanks,  
> Ali

--

---

<div class="post-metadata">

**Author:** ![Ali\_3](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@Ali\_3](https://discuss.elastic.co/u/Ali_3)\
**Post date:** [November 8, 2012, 6:04pm UTC](https://discuss.elastic.co/t/newbie-question/9621/3 "2012-11-08T18:04:45Z")

</div>

Hi Simon,

Thanks for your response. It looks like this may not suffice my use case.  
What I want to do is somewhat the opposite, that is  
I store a bunch of polygons and then query for polygons that include a  
given point (lat, long). Please correct me if I misunderstood.  
Thanks for your help in advance.

-Ali

On Thursday, November 8, 2012 1:50:01 AM UTC-8, simonw wrote:

> hey,
> 
> take a look at  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-polygon-filter.html) maybe  
> this is sufficient for your usecase?
> 
> simon
> 
> On Thursday, November 8, 2012 7:37:03 AM UTC+1, Ali wrote:
> 
> > Hello,  
> > Thanks in advance for looking at my question.  
> > I would like to store a collection of polygons (each point of the polygon  
> > being a lat, long position). Next, given a point (lat, long) say p I would  
> > like to know which polygons enclose/contain point p.  
> > Does Elasticsearch support such a query? If not what is the closest  
> > thing Elasticsearch can support?
> > 
> > Thanks,  
> > Ali

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 8, 2012, 6:07pm UTC](https://discuss.elastic.co/t/newbie-question/9621/4 "2012-11-08T18:07:38Z")

</div>

Hi Ali

> Thanks for your response. It looks like this may not suffice my use  
> case. What I want to do is somewhat the opposite, that is  
> I store a bunch of polygons and then query for polygons that include a  
> given point (lat, long). Please correct me if I misunderstood.  
> Thanks for your help in advance.

The geoshape support in v0.20 sounds like what you need:

> <https://github.com/elastic/elasticsearch/issues/2169>
>
> A new spatial module was added to Lucene 4 this year which added support for sea…rching not just for points, but for general shapes including rectangles and polygons. Some of this code can be ported to ES and improved further, so that users can index and search for shapes.
> \### Shape representation
> 
> To efficiently represent shapes in the index, Shapes are converted into a series of hashes representing grid squares using implementations of a PrefixTree. The tree aspect comes from the fact that the PrefixTree represents the planet with multiple grid layers each representing a different level of precision. For example, the PrefixTree implemenetation QuadPrefixTree represents the Earth with four grid squares, labelled A, B, C and D, at its first level of precision. Move one level of precision further and there is now 16 grid squares, AA, AB, AC, AD, BA... and so on. By having this multiple layers of precision, we can create the most efficient representation of a shape which balances number of terms vs. precision
> 
> As mentioned, there are multiple PrefixTree implemenetations. Two are provided:
> \- GeohashPrefixTree - As its name suggests, uses geohashes which have more grid squares per layer
> \- QuadPrefixTree - As mentioned, represents the Earth as a quadtree and uses A, B, C and D for hashes.
> \### Search Logic
> 
> Put simply, searches for shapes are implemented by converting the query shape into grid hashes as well, and then querying for those documents which have one or many (or none) of the query shape hashes. This allows us to efficiently find those shapes which intersect or are disjoint of. Other relationships between Shapes such as contains, as discussed below, also build upon this logic.
> \### GeoShapeFieldMapper
> 
> Indexing Shapes is handled through the GeoShapeFieldMapper (known as \`geo\_shape\`). The Mapper can be configured with the following options:
> \- \`tree\` - Name of the PrefixTree implementation to be used. Currently \`geohash\` and \`quadtree\` are supported. Defaults to \`geohash\`.
> \- \`tree\_levels\` - Maximum number of levels to be used in the PrefixTree. This can be used to control how precise the representation of Shapes should be. Defaults to the default value of the chosen PrefixTree implementation.
> \- \`distance\_error\_pct\` - Used as a guide to the PrefixTree for how precise its representation should be. Defaults to 0.025 (2.5%) however 0 is also supported.
> 
> Shapes are defined in index requests using the GeoJSON format as follows:
> 
> \`\`\`
> {
> "location" : {
> "type" : "point",
> "coordinates" : \[45.0, -45.0\]
> }
> }
> \`\`\`
> 
> Currently the GeoJSON shape types \`MultiPolygon\`, \`MultiLineString\` and \`GeometryCollection\` are unsupported. The additional shape type \`envelope\` has been added which accepts two coordinates representing the upper left and bottom right of the shape respectively. Support for \`MultiPolygon\` will be added in the future.
> 
> Also note that currently Polygons cannot cross the dateline. This is in the process of being fixed.
> 
> Due to the complexity of the shape representation, the shape for a document can only be retrieved through the document's \`source\`.
> \### GeoShapeFilterParser & GeoShapeQueryParser
> 
> Filtering and querying Shapes is supported through the GeoShapeFilterParser and GeoShapeQueryParser, both denoted \`geo\_shape\`. Both accept, in addition to the usual caching parameters for filtering and boost for querying:
> \- \`shape\` - The GeoJSON representation of the query shape, following the same syntax as used in the Mapper
> \- \`relation\` - The kind of filter / query to be executed, as represented by a relationship between the query and indexed shapes. Currently supported values are \`intersects\`, \`disjoint\` and \`contains\`. See more information below.
> 
> The following is an example of this syntax:
> 
> \`\`\`
> {
> "location" : {
> "shape" : {
> "type" : "envelope",
> "coordinates" : \[ 
> \[-45.0, 45.0\], \[45.0, -45.0\] 
> \]
> },
> "relation" : "contains"
> }
> }
> \`\`\`
> \#### Shape Relations
> 
> Currently three shape relationships are supported:
> \- \`intersects\` - Finds those indexed shapes which intersect with the query shape. Intersection occurs when the two shapes have at least one shared grid hash. Because of current limitations of the algorithm, very large indexed shapes are not deemed to intersect with very small query shapes. However, smaller indexed shapes will intersect with larger query shapes
> \- \`disjoint\` - Finds those indexed shapes which are disjoint to the query shape. This means the the indexed shapes and query shape must have no shared grid hashes.
> \- \`contains\` - Finds those indexed shapes which are fully contained within the query shape. Unlike \`intersects\`, this means that all of the indexed shape must be present in the query shape. Any shapes which have additional area outside of the query shape are excluded. This relationship is still experimental and due to its use of polygons, the query shape cannot be within 0.5 of the dateline.
> \### Future improvements
> 
> Future improvements for this feature are:
> \- Currently queries created by the GeoShapeQueryParser are wrapped in a ConstantScoreQuery. With integration into ES's in memory caching, it would be possible to cache the Shapes and calculate more useful scores based on distance or overlap percetange.
> \- Multipolygon support
> \- Handling of polygons which cross the dateline
> \- Support for adding and using named shapes. This would mean that users wouldn't have to define the coordinates for their shapes everytime, they would only need to configure a shape once with a name and would then be able to reference it.

Should be released in the next few weeks I'd imagine

clint

> -Ali
> 
> On Thursday, November 8, 2012 1:50:01 AM UTC-8, simonw wrote:  
> hey,
> 
> ```
> take a look
> at http://www.elasticsearch.org/guide/reference/query-dsl/geo-polygon-filter.html maybe this is sufficient for your usecase?
>     
>     
> simon
>     
> On Thursday, November 8, 2012 7:37:03 AM UTC+1, Ali wrote:
> Hello, 
> Thanks in advance for looking at my question.
> I would like to store a collection of polygons (each
> point of the polygon being a lat, long position).
> Next, given a point (lat, long) say p I would like to
> know which polygons enclose/contain point p.
> Does elastic search support such a query? If not what
> is the closest thing elastic search can support?
>             
>             
> Thanks,
> Ali
> 
> ```
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 8, 2012, 6:12pm UTC](https://discuss.elastic.co/t/newbie-question/9621/5 "2012-11-08T18:12:36Z")

</div>

v0.20 should have been released this week since there were no issues with  
the release candidates. I have no idea when it will actually be released,  
but hopefully it won't be a few weeks.

RC1 is available in Maven/GitHub.

--  
Ivan

On Thu, Nov 8, 2012 at 10:07 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> The geoshape support in v0.20 sounds like what you need:
> 
> [Support GeoShape searching and indexing · Issue #2169 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2169)
> 
> Should be released in the next few weeks I'd imagine
> 
> clint

--

---

<div class="post-metadata">

**Author:** ![Ali\_3](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@Ali\_3](https://discuss.elastic.co/u/Ali_3)\
**Post date:** [November 8, 2012, 6:22pm UTC](https://discuss.elastic.co/t/newbie-question/9621/6 "2012-11-08T18:22:37Z")

</div>

Thank you Clint, that should solve my use case.  
-Ali

On Thursday, November 8, 2012 10:07:53 AM UTC-8, Clinton Gormley wrote:

> Hi Ali
> 
> > Thanks for your response. It looks like this may not suffice my use  
> > case. What I want to do is somewhat the opposite, that is  
> > I store a bunch of polygons and then query for polygons that include a  
> > given point (lat, long). Please correct me if I misunderstood.  
> > Thanks for your help in advance.
> 
> The geoshape support in v0.20 sounds like what you need:
> 
> [Support GeoShape searching and indexing · Issue #2169 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2169)
> 
> Should be released in the next few weeks I'd imagine
> 
> clint
> 
> > -Ali
> > 
> > On Thursday, November 8, 2012 1:50:01 AM UTC-8, simonw wrote:  
> > hey,
> > 
> > ```
> > take a look 
> > at 
> > 
> > ```
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-polygon-filter.htmlmaybe) this is sufficient for your usecase?
> 
> > ```
> > simon 
> >     
> > On Thursday, November 8, 2012 7:37:03 AM UTC+1, Ali wrote: 
> > Hello, 
> > Thanks in advance for looking at my question. 
> > I would like to store a collection of polygons (each 
> > point of the polygon being a lat, long position). 
> > Next, given a point (lat, long) say p I would like to 
> > know which polygons enclose/contain point p. 
> > Does elastic search support such a query? If not what 
> > is the closest thing elastic search can support? 
> >             
> >             
> > Thanks, 
> > Ali 
> > 
> > ```
> > 
> > --

--

---

<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:05am UTC](https://discuss.elastic.co/t/newbie-question/9621/7 "2017-07-06T03:05:15Z")

</div>


