elasticsearch 7.x geographical distance filtering geo_distance uses -- nearby stores, nearby people, etc.

Get the points falling in the circle with the specified longitude and latitude as the center and the specified distance as the radius.

Get geographical coordinates

Add index mapping, type geo_point needs to be set manually
PUT /address
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "name": {
    
    
        "type": "text"
      },
      "location": {
    
    
        "type": "geo_point"
      }
    }
  }
}
Add test data

Note: If geo_point is a string or object, latitude is first, then longitude. If geo_point is an array, longitude is first, then latitude.

POST /address/_doc/1
{
    
    
  "name": "广州公园前",
  "location": "23.131479, 113.270719"
}

POST /address/_doc/2
{
    
    
  "name": "广百百货",
  "location": "23.129881, 113.274646"
}

PUT /address/_doc/3 
{
    
    
  "name": "广州市妇幼医院",
  "location": "23.129785, 113.260777"
}

POST /address/_doc/4 
{
    
    
  "name": "广州市银河烈士陵园",
  "location": "23.168615, 113.343607"
}
Find documents with coordinates (latitude, longitude) "23.131304,113.262402" within 1 km

_geo_distance: used to calculate the distance between two points
unit: the specified unit is km, the default is m

GET /restaurant/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "filter": {
    
    
        "geo_distance": {
    
    
          "distance": "1km",
          "location": {
    
    
            "lat": 23.131304,
            "lon": 113.262402
          }
        }
      }
    }
  },
  "sort": [
    {
    
    
      "_geo_distance": {
    
    
        "unit": "km", 
        "location": {
    
    
          "lat": 23.131304,
          "lon": 113.262402
        },
        "order": "asc"
      }
    }
  ]
}

Find results
{
    
    
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
    
    
        "_index" : "restaurant",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
    
    
          "name" : "广州市妇幼医院",
          "location" : "23.129785, 113.260777"
        },
        "sort" : [
          0.23694189365651083
        ]
      },
      {
    
    
        "_index" : "restaurant",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
    
    
          "name" : "公园前",
          "location" : "23.131479, 113.270719"
        },
        "sort" : [
          0.8506760921207346
        ]
      }
    ]
  }
}

Guess you like

Origin blog.csdn.net/coffee_shop/article/details/107110707