熟练掌握ES Java API基于地理位置对周围汽车4S店进行搜索

比如我们有很多的4S店,然后给了用户一个APP,在某个地方的时候,可以根据当前的地理位置搜索一下,自己附近的4S店

PUT /car_shop_location
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  },
  "mappings": {
    "shops": {
      "properties": {
        "pin": {
          "properties": {
            "location": {
              "type": "geo_point"
            }
          }
        }
      }
    }
  }
}
PUT car_shop_location/shops/1
{
  "name": "深圳宝马4S店",
  "pin": {
    "location": {
      "lat": 40.12,
      "lon": -71.34
    }
  }
}

1、搜索两个坐标点组成的一个区域
2、指定一个区域,由三个坐标点组成
3、搜索距离当前位置在200km内的4S店

        Settings settings = Settings.builder()
                .put("cluster.name", "my-application")
                .put("client.transport.sniff", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings).
                addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.80.188"), 9300));
        SearchResponse searchResponse = client.prepareSearch("car_shop_location")
                .setTypes("shops")
                .setQuery(QueryBuilders.geoBoundingBoxQuery("pin.location").
                        setCorners(40.73, -74.1, 40.01, -71.12))
                .get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("==================================我是华丽的分割线==================================");
        List points = new ArrayList<>();
        points.add(new GeoPoint(40.73, -74.1));
        points.add(new GeoPoint(40.01, -71.12));
        points.add(new GeoPoint(50.56, -90.58));
        searchResponse = client.prepareSearch("car_shop_location")
                .setTypes("shops")
                .setQuery(QueryBuilders.geoPolygonQuery("pin.location", points))
                .get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("==================================我是华丽的分割线==================================");
        searchResponse = client.prepareSearch("car_shop_location")
                .setTypes("shops")
                .setQuery(QueryBuilders.
                        geoDistanceQuery("pin.location")
                        .point(40, -70)
                        .distance(200, DistanceUnit.KILOMETERS))
                .get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        client.close();

你可能感兴趣的:(熟练掌握ES Java API基于地理位置对周围汽车4S店进行搜索)