Reverse geo distance search

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

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

Welcome!

Geo_shapes are supporting circles. See Geo-shape field type | Elasticsearch Reference [7.10] | Elastic

Would that work for you?

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

Here there is a "envelope" filter.
image

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
}

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?

@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 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

1 Like

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"
            }
          }
        }
      ]
    }
  }
}
1 Like

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

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 and here

" 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 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
          }
        }
      }
    ]
  }
}

Hi @Fatih_Kurnaz

A little more, if you want to use the non-deproceated approach see here: 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

Thanks a lot. All of them are helpfull

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