Pierre
Pierre

Reputation: 21

elasticserarch geo distance filter but distance is on documen instead of in the query

I have entrys with "xyz" willing to drive a certain distance to a persion.

the range is on the document instead the other way round

how would i be able to structure a query to fullfill this requirement?

thanks

my records look pretty much like this

{
  "name": "XYZ",
  "pin": {
"name": "Bradford",
"location": {
  "lat": 51.5,
  "lon": 0.1
}
  },
  "max_travel_radius": "25km"
}

the regular geo distance query would yield wrong results because its not about how far away the "searcher" is from the entry

the entry has a radius and can be reached by the "searcher"

Upvotes: 2

Views: 81

Answers (1)

nitzien
nitzien

Reputation: 1267

You need to create your document having geo_shape instead of a location. Geo_shape that you should use is circle, where you specify person location and max_travel_distance as radius. Please check out circle at this url. Your input document may look like

{
  "name": "XYZ",
  "pin": {
      "name": "Bradford",
     "location": {
        "type": circle, 
        "coordinates" : [101.0, 1.0],
        "radius" : "25000m"
      }
  }
}

Next you should used geo_shape query to check if a point lies WITHIN in the shape of document you created above. A sample query will be like (You will need to change it for nested query. Just giving an example here)

GET /example/_search
{
    "query":{
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "point",
                            "coordinates" : [102.0, 2.0]
                        },
                        "relation": "contains"
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions