# Incorrect geo\_shape results in both es6.x and 7.x

**URL:** <https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386>\
**Category:** Elasticsearch\
**Created:** [August 15, 2019, 7:39pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386 "2019-08-15T19:39:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 15, 2019, 7:39pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/1 "2019-08-15T19:39:56Z")

</div>

I'm still seeing the same incorrect shape query behavior that I saw back to ES 1.7 after starting fresh on es 6.7 and 7.x (hosted by [elastic.co](http://elastic.co)). Here is the relevant data:

Documents with WKT shape data for NY and CT states that look like this:  
NY:

 ![13%20PM](https://us1.discourse-cdn.com/elastic/original/3X/3/7/37e1f4db652f0ac7a7fe83e38350d3be249f4148.jpeg)  
CT:  
 ![19%20PM](https://us1.discourse-cdn.com/elastic/original/3X/5/e/5e3c12c60a086786f33522039d61c3a6ecd241a1.png)

and I'm using this query to look for shapes that contain this point, clearly within CT and not within NY state:

```
GET /senate/_search
{
"query":{
    "bool": {
        "must": {
            "match_all": {}
        },
        "filter": {
            "geo_shape": {
                "shape": {
                    "shape": {
                        "type": "point",
                        "coordinates" : [-73.572866,41.142307]
                    }
                }
            }
        }
    }
}}

```

The point is located in North Stamford on a map, and to be honest not really that close to NY.

 ![43%20PM](https://us1.discourse-cdn.com/elastic/original/3X/4/9/496a6b85ef366e10119cb0020921c214b7eef653.jpeg)

This query returns documents with BOTH NY and CT shapes whereas it should only return documents with CT shapes.

@spinscale any idea why this is happening or how I can get actual geo\_shape queries that aren't some approximation of a shape? I don't care if it takes an extra 500ms, I just want the queries to return the accurate data that they clearly should.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 15, 2019, 10:54pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/2 "2019-08-15T22:54:02Z")

</div>

Perhaps you need to use the relation INTERSECTS

`"relation": "intersects"`

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html)

Note / edit yes that is supposed to be the default but you might try being explicit

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 15, 2019, 11:09pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/3 "2019-08-15T23:09:45Z")

</div>

Hi Stephen,

Same incorrect results setting the relation specifically to "intersect".

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 15, 2019, 11:56pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/4 "2019-08-15T23:56:39Z")

</div>

Asking internally....

What happens with..

`"relation": "contains"`

Apologies for not being as helpful as I would like....

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 16, 2019, 1:26am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/5 "2019-08-16T01:26:25Z")

</div>

Same results for "contains" (and "within" just for fun). Thanks for asking around.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 16, 2019, 3:25am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/6 "2019-08-16T03:25:08Z")

</div>

Can you post your mapping for the state shape data

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 16, 2019, 3:43am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/7 "2019-08-16T03:43:31Z")

</div>

Relevant shape property is this one:

```
	"properties":{
		"shape":{
			"type":"geo_shape",
			"strategy" : "recursive"
		}
	}
```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 16, 2019, 5:17am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/8 "2019-08-16T05:17:40Z")

</div>

So here is my working version.

What I did is create 2 simple polygons called the CT and NY (see images at the bottom)  
Then created 3 queries 1 in CT, 1 in NY and 1 in neither all are working as expected.  
I think I would stay away from the strategy stuff as it is all deprecated.

See if you can repeat

Here is all my code........note I am only returning the name with the `"_source": ["name"],` directive in the query so as not to return big polygons... you can take that out if you want to see my polygon results

```
# GEO STUFF
DELETE /regions

PUT /regions
{
  "mappings": {
    "properties": {
      "name" : {"type" : "text"},
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

GET /regions/_mapping

{
  "regions" : {
    "mappings" : {
      "properties" : {
        "location" : {
          "type" : "geo_shape"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

POST /regions/_doc
{
  "name": "CT",
  "location": {
    "type": "polygon",
    "coordinates": [
      [
        [
          -73.7268427,
          41.1024415
        ],
        [
          -73.6533716,
          41.0076869
        ],
        [
          -73.3979395,
          41.0931274
        ],
        [
          -73.4830835,
          41.2115246
        ],
        [
          -73.7268427,
          41.1024415
        ]
      ]
    ]
  }
}

POST /regions/_doc
{
  "name": "NY",
  "location": {
    "type": "polygon",
    "coordinates": [
      [
        [
          -73.7268427,
          41.1024415
        ],
        [
          -73.4849353,
          41.2137893
        ],
        [
          -73.5515399,
          41.2974142
        ],
        [
          -73.797359,
          41.2143058
        ],
        [
          -73.7268427,
          41.1024415
        ]
      ]
    ]
  }
}

# Point in CT
GET /regions/_search
{
  "_source": ["name"],
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            -73.5576296,
            41.1012336
          ]
        }
      }
    }
  }
}

# Point in NY
GET /regions/_search
{
  "_source": ["name"],
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            -73.6490436,
            41.2075906
          ]
        }
      }
    }
  }
}

# Point in Niether
GET /regions/_search
{
  "_source": ["name"],
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            -73.7190814,
            41.3293904
          ]
        }
      }
    }
  }
}

# Point in CT with bool
GET /regions/_search
{
  "_source": [
    "name"
  ],
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "point",
              "coordinates": [
                -73.5576296,
                41.1012336
              ]
            }
          }
        }
      }
    }
  }
}

# NY with Bool 
GET /regions/_search
{
  "_source": ["name"],
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "point",
              "coordinates": [
                -73.6490436,
                41.2075906
              ]
            }
          }
        }
      }
    }
  }
}

# Neither
GET /regions/_search
{
  "_source": [
    "name"
  ],
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "point",
              "coordinates": [
                -73.7190814,
                41.3293904
              ]
            }
          }
        }
      }
    }
  }
}

```

Here are the polygons / images  
CT

 ![41%20PM](https://us1.discourse-cdn.com/elastic/original/3X/5/d/5d3c6ea9ec08807f56b5e53684819a9c85f2ad61.jpeg)

NY

 ![11%20PM](https://us1.discourse-cdn.com/elastic/original/3X/a/1/a1e53af10e185d5fa7065644925bf5db859ae8cc.jpeg)

Edit : Fixed Code... all working now

Note : I did a little more complex NY with it "surrounding" CT and it works ... any possibility that there is an issue importing the WKTs? You know in 7.3 you can easily display them in the Maps Application example (The Neither Point is now in NY with this example)

 ![22%20PM](https://us1.discourse-cdn.com/elastic/original/3X/1/6/16a941eba60680b23aaa1e21c4bd6281b8bf5ec4.jpeg)

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 16, 2019, 3:06pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/9 "2019-08-16T15:06:53Z")

</div>

Hi Stephen,

There are plenty of points that appear to work correctly in my version as well, something like `-73.554212, 41.053923` gives me results from inside CT only. My question is why there are some points that do _not_ work correctly with these non-overlapping shapes.

WKTs for NY and CT follow:

`POLYGON((-73.343806 45.013027,-73.332852 44.804903,-73.387622 44.618687,-73.294514 44.437948,-73.321898 44.246255,-73.436914 44.043608,-73.349283 43.769761,-73.404052 43.687607,-73.245221 43.523299,-73.278083 42.833204,-73.267129 42.745573,-73.508114 42.08834,-73.486206 42.050002,-73.55193 41.294184,-73.48073 41.21203,-73.727192 41.102491,-73.655992 40.987475,-73.22879 40.905321,-73.141159 40.965568,-72.774204 40.965568,-72.587988 40.998429,-72.28128 41.157261,-72.259372 41.042245,-72.100541 40.992952,-72.467496 40.845075,-73.239744 40.625997,-73.562884 40.582182,-73.776484 40.593136,-73.935316 40.543843,-74.022947 40.708151,-73.902454 40.998429,-74.236547 41.14083,-74.69661 41.359907,-74.740426 41.431108,-74.89378 41.436584,-75.074519 41.60637,-75.052611 41.754247,-75.173104 41.869263,-75.249781 41.863786,-75.35932 42.000709,-79.76278 42.000709,-79.76278 42.252649,-79.76278 42.269079,-79.149363 42.55388,-79.050778 42.690804,-78.853608 42.783912,-78.930285 42.953697,-79.012439 42.986559,-79.072686 43.260406,-78.486653 43.375421,-77.966344 43.369944,-77.75822 43.34256,-77.533665 43.233021,-77.391265 43.276836,-76.958587 43.271359,-76.695693 43.34256,-76.41637 43.523299,-76.235631 43.528776,-76.230154 43.802623,-76.137046 43.961454,-76.3616 44.070993,-76.312308 44.196962,-75.912491 44.366748,-75.764614 44.514625,-75.282643 44.848718,-74.828057 45.018503,-74.148916 44.991119,-73.343806 45.013027))`

`POLYGON((-73.053528 42.039048,-71.799309 42.022617,-71.799309 42.006186,-71.799309 41.414677,-71.859555 41.321569,-71.947186 41.338,-72.385341 41.261322,-72.905651 41.28323,-73.130205 41.146307,-73.371191 41.102491,-73.655992 40.987475,-73.727192 41.102491,-73.48073 41.21203,-73.55193 41.294184,-73.486206 42.050002,-73.053528 42.039048))`

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 16, 2019, 4:10pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/10 "2019-08-16T16:10:51Z")

</div>

Can you provide several points that do not work actual and expected results. If / when I can... I will take a look.

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 16, 2019, 9:32pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/11 "2019-08-16T21:32:09Z")

</div>

Points that work correctly:  
`-73.554212, 41.053923` (stamford, CT)  
`-73.624998,41.047451` (greenwich, CT)  
`-73.708180,41.126757` (armonk, NY)

Points that do not work correctly:  
`-73.572866,41.142307` (north stamford, CT)  
`-73.681591,41.098201` (greenwich, CT)  
`-73.683025,41.017221` (rye brook, NY)

Some points that work are close to the border, some points that do not work are farther away. It definitely seems like the query happens on a different (possibly simplified?) shape than the one you would expect from viewing the WKT shape.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 17, 2019, 4:47am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/12 "2019-08-17T04:47:57Z")

</div>

@nickoneill  
Interesting... I am having the exact opposite experience using 7.3 .... I put in the WKT Regions and all the searches you provided return correct results and I can not get a bad result randomly and when I zoom down within meters it seems the results are still good until I place a point almost directly on top of the line.. that is not to say that there is not an issue.

```
DELETE /regions

PUT /regions
{
  "mappings": {
    "properties": {
      "name" : {"type" : "text"},
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

POST /regions/_doc
{
  "name": "NY",
  "location": "POLYGON((-73.343806 45.013027,-73.332852 44.804903,-73.387622 44.618687,-73.294514 44.437948,-73.321898 44.246255,-73.436914 44.043608,-73.349283 43.769761,-73.404052 43.687607,-73.245221 43.523299,-73.278083 42.833204,-73.267129 42.745573,-73.508114 42.08834,-73.486206 42.050002,-73.55193 41.294184,-73.48073 41.21203,-73.727192 41.102491,-73.655992 40.987475,-73.22879 40.905321,-73.141159 40.965568,-72.774204 40.965568,-72.587988 40.998429,-72.28128 41.157261,-72.259372 41.042245,-72.100541 40.992952,-72.467496 40.845075,-73.239744 40.625997,-73.562884 40.582182,-73.776484 40.593136,-73.935316 40.543843,-74.022947 40.708151,-73.902454 40.998429,-74.236547 41.14083,-74.69661 41.359907,-74.740426 41.431108,-74.89378 41.436584,-75.074519 41.60637,-75.052611 41.754247,-75.173104 41.869263,-75.249781 41.863786,-75.35932 42.000709,-79.76278 42.000709,-79.76278 42.252649,-79.76278 42.269079,-79.149363 42.55388,-79.050778 42.690804,-78.853608 42.783912,-78.930285 42.953697,-79.012439 42.986559,-79.072686 43.260406,-78.486653 43.375421,-77.966344 43.369944,-77.75822 43.34256,-77.533665 43.233021,-77.391265 43.276836,-76.958587 43.271359,-76.695693 43.34256,-76.41637 43.523299,-76.235631 43.528776,-76.230154 43.802623,-76.137046 43.961454,-76.3616 44.070993,-76.312308 44.196962,-75.912491 44.366748,-75.764614 44.514625,-75.282643 44.848718,-74.828057 45.018503,-74.148916 44.991119,-73.343806 45.013027))"
}

POST /regions/_doc
{
  "name": "CT",
  "location": "POLYGON((-73.053528 42.039048,-71.799309 42.022617,-71.799309 42.006186,-71.799309 41.414677,-71.859555 41.321569,-71.947186 41.338,-72.385341 41.261322,-72.905651 41.28323,-73.130205 41.146307,-73.371191 41.102491,-73.655992 40.987475,-73.727192 41.102491,-73.48073 41.21203,-73.55193 41.294184,-73.486206 42.050002,-73.053528 42.039048))"
  
}

```

All these searches return correct results a created them as points to view as well

-73.554212, 41.053923 (stamford, CT)  
-73.624998,41.047451 (greenwich, CT)  
-73.708180,41.126757 (armonk, NY)

These Points work correctly as well  
-73.572866,41.142307 (north stamford, CT)  
-73.681591,41.098201 (greenwich, CT)  
-73.683025,41.017221 (rye brook, NY)

Using this query and substituting in each in turn I got correct results for each. I also put in a number very close to the borders and they were correct.

north stamford : correct

```
GET /regions/_search
{
  "_source": [
    "name"
  ],
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "point",
              "coordinates": [
                -73.572866,
                41.142307
              ]
            }
          }
        }
      }
    }
  }
}

```

results for north stamford etc

```
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "regions",
        "_type" : "_doc",
        "_id" : "GFC8nWwBvL0x7al8Ij6K",
        "_score" : 1.0,
        "_source" : {
          "name" : "CT"
        }
      }
    ]
  }
}

```

Not sure what to tell you / think at this point. This is all on 7.3...

 ![23%20PM](https://us1.discourse-cdn.com/elastic/original/3X/b/1/b19f52bbe96811cc7695182d3e65b86635a706fd.jpeg)

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 17, 2019, 10:32pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/13 "2019-08-17T22:32:55Z")

</div>

Thanks for testing again Stephen, this works as expected without the `strategy` for the `geo_shape` mapping set to `recursive`.

I was hoping to use the `circle` shape for some queries. Without `recursive`, using `circle` in queries returns "CIRCLE geometry is not supported." I can work around this for now.

Do you know the technical details of why `recursive` makes this behave differently?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 17, 2019, 11:09pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/14 "2019-08-17T23:09:33Z")

</div>

No that is beyond my spatial knowledge at this point (minor pun) but if I have time I will look into it. So is the circle / radius search within the larger polygon the important aspect of what you are trying to accomplish. often I see circle / radius searches that are larger than smaller polygons like property outlines looking for intersection , seems like you are trying to do a small circle/ radius within the larger polygons is that correct? I think I'm lacking insight on what that accomplishes for your use case. What is your expectation when the circle intersects more than 1 polygon.

[Note](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#prefix-trees): it does look like we are in the middle of a major re-engineering of the spatial engine that is kind of indicated in the docs, some features are still there and marked deprecated as the new engine is moving towards more full features / feature parity with improved performance and accuracy.

---

<div class="post-metadata">

**Author:** ![nickoneill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickoneill/32/48175_2.png) [@nickoneill](https://discuss.elastic.co/u/nickoneill)\
**Post date:** [August 18, 2019, 5:47pm UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/15 "2019-08-18T17:47:22Z")

</div>

It's not really important to use `circle` for me, just one of the use cases I was testing out.

I'm all set here, thank you for your perseverance helping me with this issue @stephenb!

---

<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:** [November 4, 2022, 7:21am UTC](https://discuss.elastic.co/t/incorrect-geo-shape-results-in-both-es6-x-and-7-x/195386/16 "2022-11-04T07:21:32Z")

</div>


