比如我们有很多的4s店,然后呢给了用户一个app,在某个地方的时候,可以根据当前的地理位置搜索一下,自己附近的4s店
相关es restful api 可以参考:
《Elasticsearch 之(39)引入geo point地理位置数据类型_矩形范围查询》
<dependency> <groupId>org.locationtech.spatial4j</groupId> <artifactId>spatial4j</artifactId> <version>0.6</version> </dependency> <dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts</artifactId> <version>1.13</version> <exclusions> <exclusion> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> </exclusion> </exclusions> </dependency>mapping
POST /car_shop/_mapping/shops { "properties": { "pin": { "properties": { "location": { "type": "geo_point" } } } } }初始化数据
PUT /car_shop/shops/1 { "name": "上海至全宝马4S店", "pin" : { "location" : { "lat" : 40.12, "lon" : -71.34 } } } PUT /car_shop/shops/2 { "name": "上海顺风宝马4S店", "pin" : { "location" : { "lat" : 40.12, "lon" : -71.34 } } }
查询一定数量坐标点组成的范围内的4S店,以及一定距离内的4S店
package com.es.app; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; import java.util.ArrayList; import java.util.List; public class GeoLocationShopSearchApp { @SuppressWarnings({ "resource", "unchecked" }) public static void main(String[] args) throws Exception { Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); //第一个需求:搜索两个坐标点组成的一个区域 SearchResponse searchResponse = client.prepareSearch("car_shop") .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<GeoPoint> 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") .setTypes("shops") .setQuery(QueryBuilders.geoPolygonQuery("pin.location", points)) .get(); for(SearchHit searchHit : searchResponse.getHits().getHits()) { System.out.println(searchHit.getSourceAsString()); } System.out.println("===================================================="); //第三个需求:搜索距离当前位置在200公里内的4s店 searchResponse = client.prepareSearch("car_shop") .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 restful api 可以参考:
《Elasticsearch 之(39)引入geo point地理位置数据类型_矩形范围查询》
《Elasticsearch 之(40)酒店o2o搜索案例以及搜索指定区域内的酒店》
《Elasticsearch 之(41)搜索距离当前位置一定范围内的酒店》
《Elasticsearch 之(41)搜索距离当前位置一定范围内的酒店》