ElasticSearch Reference [7.10] SELECT * FROM ‘Quaseries’ WHERE (1) [TOC]

First, prepare data

1.1,geo_point

The special type GEO_POINT in ES is used to store the map type, which is delivered on a certain platform. Express delivery personnel reflected from you how many meters, is this data type to make.

1.2. Create index map

PUT /map
{
  "settings": {
    "number_of_replicas": 3,
    "number_of_shards": 5
  }
  , "mappings": {
    "properties": {
      "name":
      {
        "type": "text"
      }
      ,"location":
      {
        "type": "geo_point"
      }
    }
  }
}

1.3. Add data

PUT /map/_doc/1 {"name":" Tiananmen Square ", "location": {" lon ": 116.403981," lat ": 39.914492}} PUT/map / _doc / 2 {" name" : "haidian park", "location" : {" lon ": 116.302509," lat ": 39.991152}} PUT/map / _doc / 3 {" name" : "Beijing zoo", "location" : {"lon":116.343184, "lat":39.947468}}

II. The map retrieval method of ES

The following methods of map retrieval are supported by ES;

  • geo_distance

    • geo_bounding_box
    • geo_polygon

2.1,geo_distance: Straight distance search, such as giving point A, and asking to return to the merchants on the map that are three kilometers away from point A (ordering takeout scene)

2.1.1 Find the point within 3000 meters from Beijing Railway Station (116.433733,39.908404)

geo_distanceThe parameters involved are as follows

  • Location: to identify a point;
  • Distance: Determine a radius, in meters
  • Distance_type: Determines the type of a figure, which is generally circular,arc

2.1.1.1 RESTful code

POST /map/_search { "query": { "geo_distance": { "location": {" lon ": 116.433733," lat ": 39.908404}," short ": 3000," distance_type ":" arc "}}}

2.1.1.2 Java code implementation

package com.chb.test; import com.chb.utils.ESClient; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Test; import java.io.IOException; public class GeoDemo { RestHighLevelClient client = ESClient.getClient(); String index = "map"; @test public void GeoDistance () throws IOException {@test public void GeoDistance () throws IOException {@test public void GeoDistance () throws IOException SearchRequest(index); // 2, SearchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder. Query (QueryBuilders. GeoDistanceQuery (" location "). The short (" 3000 "). The point (39.908404, 116.433733)); // Add the condition to the Request object searchRequest.source(searchSourceBuilder); SearchResponse = Client. Search (SearchRequest, RequestOptions. Default); // for (searchHit Hit: resp.gethits ().gethits ()) {system.out.println (hit.getSourceAseasMap ()); }}}

2.2,geo_bounding_box: Determining a rectangle with two points to get all the data within the rectangle

2.2.1,geo_bounding_boxThe parameters involved are as follows

  • top_left: latitude and longitude of the starting point of the rectangle in the upper left corner;
  • bottom_right: Longitude and latitude of the end points of the rectangle in the lower right corner

SELECT * FROM ‘Minzu University of China’ WHERE (116.326943,39.95499) AND (116.433733,39.908404)

2.2.2.1, RESTful code

POST /map/_search {"query": {"geo_bounding_box": {"location": {"top_left": {"lon": 116.326943, "lat": 39.95499}, "bottom_right": {"lon": 116.433446, "lat": 39.908737}}}}}

2.2.2.2 Java code

// 2, SearchSourceBuilder = new SearchSourceBuilder(); // ======= to change the code SearchSourceBuilder. Query (QueryBuilders geoBoundingBoxQuery (" location "). SetCorners (39.95499, 116.326943, 39.908737, 116.433 446)); / / = = = = = = = = = = = = = = = = = = = = = / / add conditions to the request object the searchRequest. Source (searchSourceBuilder);

2.3,geo_polygon: With multiple points, determine the polygon and get all the data in the polygon

2.3.1 The parameters of geo_polygon are as follows

Points: An array containing the longitude and latitude of multiple morphed points, each of which is enclosed in curly braces

2.3.2 Find the points in the index of Xiyuanqiao (116.300209,40.003423), Bagou (116.29561,39.976004) and University of Science and Technology Beijing (116.364528,39.996348)

2.3.2.1, RESTful code

POST /map/_search {"query": {"geo_polygon": {"location": {"points": [{"lon": 116.29561, "lat": 39.976004}, {" lon ": 116.364528," lat ": 39.996348}, {" lon" : 116.300209, "lat" : 40.003423}]}}}}

2.3.2.2 Java code

// 2, SearchSourceBuilder = new SearchSourceBuilder(); / / = = = = = = = change code = = = = = = = = List < GeoPoint > points = new ArrayList < GeoPoint > (); Add (new Geopoint (39.976004,116.29561)); add(new Geopoint (39.976004,116.29561)); Points. The add (new GeoPoint (39.996348, 116.364528)); Points. The add (new GeoPoint (40.003423, 116.300209)); searchSourceBuilder.query(QueryBuilders.geoPolygonQuery("location", points)); / / = = = = = = = = = = = = = = = = = = = = = / / add conditions to the request object the searchRequest. Source (searchSourceBuilder);

Pay attention to my public number [big data], more dry goods