# Reverse geo distance search

**URL:** <https://discuss.elastic.co/t/reverse-geo-distance-search/263524>\
**Category:** Elasticsearch\
**Created:** [February 7, 2021, 3:02pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524 "2021-02-07T15:02:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fatih\_Kurnaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatih_kurnaz/32/83540_2.png) [@Fatih\_Kurnaz](https://discuss.elastic.co/u/Fatih_Kurnaz)\
**Post date:** [February 7, 2021, 3:02pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/1 "2021-02-07T15:02:32Z")

</div>

Hi everyone;

There are 2 indices at my system. One of them is people and another one is houses.

There is location criteria for each person at people indice like this.

```
{
    "locationName": "london",
    "distance": 1,
    "location": {
        "lon": -0.1277583,
        "lat": 51.5073509
    }
}

```

And, there is location info for house at houses indice like this.

```
{
    "location": {
        "lat": "51.6561206",
        "lon": "0.5517064"
    }
}

```

When person wants to see suitable home for him, I filter houses by using geo\_distance. everything is perfect up to this point.

However; I need to filter people who are looking for a houses in a certain CIRCLE area. For example ; A is area zone where people want to find home . B is area zone that I define .  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/c/ac985fe4afb5eb9f41a5abc16d52b94a8d769c4f.png)

How can I apply this filter? geo\_shape is not solution for me. Since it uses rectangular shape, but I need circle shape

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 7, 2021, 3:19pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/2 "2021-02-07T15:19:56Z")

</div>

Welcome!

Geo\_shapes are supporting circles. See [Geo-shape field type | Elasticsearch Reference [7.10] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#_circle)

Would that work for you?

---

<div class="post-metadata">

**Author:** ![Fatih\_Kurnaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatih_kurnaz/32/83540_2.png) [@Fatih\_Kurnaz](https://discuss.elastic.co/u/Fatih_Kurnaz)\
**Post date:** [February 7, 2021, 4:42pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/3 "2021-02-07T16:42:50Z")

</div>

Thanks a lot .  
That is good , but I need to filter them by drawing circle.

Here there is a "envelope" filter.  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/1/01726ba5dab809b8ccac480955e574fdc7efb4bc.png)

> **[Geo-shape query | Elasticsearch Reference \[7.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html#_pre_indexed_shape)**

How can I filter by drawing circle?

When I try to filter by using this code  
{  
"query": {  
"geo\_shape": {  
"location": {  
"shape": {  
"type": "circle",  
"coordinates": [  
51.5073509,  
-0.1277583  
],  
"radius": 10  
}  
}  
}  
}  
}

I am taking this error

{  
"error": {  
"root\_cause": [  
{  
"type": "unsupported\_operation\_exception",  
"reason": "CIRCLE geometry is not supported"  
}  
],  
"type": "unsupported\_operation\_exception",  
"reason": "CIRCLE geometry is not supported"  
},  
"status": 500  
}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 7, 2021, 6:02pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/4 "2021-02-07T18:02:06Z")

</div>

I'm not expert on the geo stuff so hopefully someone from the team will be able to answer why it's not supported.

In the meantime may be you can approximate the circle by drawing a polygon which is fitting inside the circle?

---

<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:** [February 7, 2021, 7:05pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/5 "2021-02-07T19:05:13Z")

</div>

@Fatih_Kurnaz it would help if you formatted the code in your post with the format button `</>`

What version are you on? I think circle support is fairly recent.

This is version 7.10.2 ... the circle query works see below  
if you want the intersection of your location points + 2 circles you will need to do a `bool` with `must_match` for both circles. An important note the the geo\_shape query is within the `filter` context see [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/query-filter-context.html) which your is not.

```
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "text": "location 1",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

PUT my-index-000001/_doc/2
{
  "text": "location 2",
  "location": { 
    "lat": 45.12,
    "lon": -71.34
  }
}

GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "circle",
              "coordinates": [-71.3, 41.1],
              "radius": "5000m"
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

```

Result

```
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "text" : "location 1",
          "location" : {
            "lat" : 41.12,
            "lon" : -71.34
          }
        }
      }
    ]
  }
}

```

Hope this helps

---

<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:** [February 7, 2021, 7:32pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/6 "2021-02-07T19:32:05Z")

</div>

Here is what I think the 2 circles would look like my coordinates are made up but this should help this will still return the first point because it lies withing both circles.

```
GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "circle",
                "coordinates": [
                  -71.3,
                  41.1
                ],
                "radius": "5000m"
              },
              "relation": "intersects"
            }
          }
        },
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "circle",
                "coordinates": [
                  -71.25,
                  41.05
                ],
                "radius": "20000m"
              },
              "relation": "intersects"
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Fatih\_Kurnaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatih_kurnaz/32/83540_2.png) [@Fatih\_Kurnaz](https://discuss.elastic.co/u/Fatih_Kurnaz)\
**Post date:** [February 8, 2021, 10:48pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/7 "2021-02-08T22:48:22Z")

</div>

Thanks a lot. My Es version is 7.x and here some part of my mapping

```
{
    "properties": {
        "location": {
            "type": "geo_shape"
        }
    }
}

```

when I try to save data , it says "CIRCLE geometry is not supported"

Here data part that I try to save

```
{
    "locationName": "london",
    "location": {
        "coordinates": [
            51.5073509,
            -0.1277583
        ],
        "type": "circle",
        "radius": "1mi"
    }
}

```

Here screenshot belongs to error

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/c/bcde1f0dabf033943030cc80ee899bc5173ff1ca.png)

---

<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:** [February 8, 2021, 11:45pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/8 "2021-02-08T23:45:03Z")

</div>

Exactly which version of 7.X are you one that covers 10 releases which is a lot of elasticsearch releases.

Please look closely are the docs [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#_circle) and [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/geo-shape.html#geoshape-indexing-approach)

_" Elasticsearch supports a `circle` type, which consists of a center point with a radius. Note that this circle representation can only be indexed when using the `recursive` Prefix Tree strategy. For the default [Indexing approach](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#geoshape-indexing-approach) circles should be approximated using a `POLYGON` ."_

So your mapping is not correct and assuming that you mean London you have the lat and lon reversed in the circles definition, and I assume you mean more than 1mi.

_For all types, both the inner `type` and `coordinates` fields are required._

_In GeoJSON and WKT, and therefore Elasticsearch, the correct **coordinate order is longitude, latitude (X, Y)** within coordinate arrays. This differs from many Geospatial APIs (e.g., Google Maps) that generally use the colloquial latitude, longitude (Y, X)._

You will notice a deprecation notice I will see if I can find out more on this... but the below all works for me.

```
DELETE my-shapes
PUT my-shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape",
        "tree": "quadtree"
      }
    }
  }
}

GET my-shapes/_search

PUT my-shapes/_doc/london
{
  "locationName": "london",
  "location": {
    "type": "circle",
    "coordinates": [
      -0.1277583,
      51.5073509
    ],
    "radius": "10000m"
  }
}

DELETE my-index-000001
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "text": "location 1",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

PUT my-index-000001/_doc/2
{
  "text": "location 2",
  "location": { 
    "lat": 45.12,
    "lon": -71.34
  }
}

PUT my-index-000001/_doc/3
{
  "text": "House in london",
  "location": { 
    "lat": 51.5,
    "lon": -0.127
  }
}

GET my-index-000001/_search

GET /my-index-000001/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_shape": {
          "location": {
            "indexed_shape": {
              "index": "my-shapes",
              "id": "london",
              "path": "location"
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

```

results

```
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "text" : "House in london",
          "location" : {
            "lat" : 51.5,
            "lon" : -0.127
          }
        }
      }
    ]
  }
}
```

---

<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:** [February 9, 2021, 3:00pm UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/9 "2021-02-09T15:00:19Z")

</div>

Hi @Fatih_Kurnaz

A little more, if you want to use the non-deproceated approach see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-circle-processor.html): It uses a circle approximation. I did all the above with the circle processor and it all works great and without the deprecation notices.

I also understand there may be some improvements on this in the future but there is not ETA

---

<div class="post-metadata">

**Author:** ![Fatih\_Kurnaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatih_kurnaz/32/83540_2.png) [@Fatih\_Kurnaz](https://discuss.elastic.co/u/Fatih_Kurnaz)\
**Post date:** [February 24, 2021, 10:30am UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/10 "2021-02-24T10:30:34Z")

</div>

Thanks a lot. All of them are helpfull

---

<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:** [March 24, 2021, 10:31am UTC](https://discuss.elastic.co/t/reverse-geo-distance-search/263524/11 "2021-03-24T10:31:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
