This is my 31st day of the August Update Challenge

The most common Elasticsearch app is the map app. A GEO is a point on the earth in terms of latitude and longitude. You can use geographic coordinates to calculate the distance between two coordinate points or calculate whether a point falls within a specific area.

Geo type declaration

Geographical coordinate points must be explicitly declared in the mapping and cannot be automatically mapped dynamically.

PUT /geo_test_index
{
  "mappings": {
      "properties": {
        "hotel_name": {
          "type": "text"
        },
        "location": {
          "type": "geo_point"
        }
      }
  }
}
Copy the code

Geo coordinate data format

Elasticsearch supports string, object, and array submission of GEO data:

  • The value is a string of characters, separated by commas (,) in latitude and longitude
  • Object form: lat explicit latitude value, LON explicit longitude value
  • Array format: longitude first latitude last

Here are examples of the three geo coordinate data formats mentioned above:

PUT /geo_test_index/_doc/1 {"city": "city a", "location": "32.231, 45.78"} PUT /geo_test_index/_doc/2 {"city": "city a", "location": "32.231, 45.78"} }} PUT /geo_test_index/_doc/3 {"city": "city c", "location": {"lat": 32.232, "lon": 45.78}} PUT /geo_test_index/_doc/3 {"city": "city c", "location": [45.78, 32.233]}Copy the code

Query data within a specified distance range

Elasticsearch allows you to query data within a radius of a specified point. The following example takes latitude 23.232 longitude 45.78 as the center point and obtains data within a radius of 20km:

GET /geo_test_index/_search { "query": { "bool": { "filter": { "geo_distance": { "distance": "20km", "location": {" LAT ": 32.232, "LON ": 45.78}}}}}}Copy the code

Gets the data query within the rectangle border

Elasticsearch not only supports querying data within a radius, but also allows you to query data within the geographic range of the rectangle constructed in the upper left and lower right corners. The following example queries the geographic range of a rectangle constructed from upper left latitude 32.3 longitude 45.7 and lower right latitude 32.1 longitude 45.8:

GET /geo_test_index/_search { "query": { "bool": { "filter": { "geo_bounding_box": { "location": { "top_left": { "lat": 32.3, "says lon" : 45.7}, "bottom_right" : {" lat ": 32.1," says lon ": 45.8}}}}}}}Copy the code

Search results are sorted by distance

In the general map application, it will be sorted by the distance to the user’s position and then returned. The following query returns data within a specific range and returns data based on proximity to a geographic location:

GET /geo_test_index/_search { "query": { "bool": { "filter": { "geo_bounding_box": { "type": "indexed", "location": {" top_left ": {" lat" : 32.3, "says lon" : 45.7}, "bottom_right" : {" lat ": 32.1," says lon ": 45.8}}}}}}," sort ": [{" _geo_distance ": {" location" : {" lat ": 32.2," says lon ": 45.76}," order ":" asc ", "unit" : "km", "distance_type" : "plane" } } ] }Copy the code