美文网首页java从0到架构师
90_熟练掌握ES Java API_基于地理位置对周围汽车4S

90_熟练掌握ES Java API_基于地理位置对周围汽车4S

作者: 小山居 | 来源:发表于2020-03-04 21:11 被阅读0次

    <meta charset="utf-8">

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

    引入地理位置引入的依赖

    image.png
    <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>
    

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

    添加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 } } }
    

    第一个需求:搜索两个坐标点组成的一个区域

    QueryBuilder qb = geoBoundingBoxQuery("pin.location").setCorners(40.73, -74.1, 40.01, -71.12);

    第二个需求:指定一个区域,由三个坐标点,组成,比如上海大厦,东方明珠塔,上海火车站

    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));

    QueryBuilder qb = geoPolygonQuery("pin.location", points);

    第三个需求:搜索距离当前位置在200公里内的4s店

    QueryBuilder qb = geoDistanceQuery("pin.location").point(40, -70).distance(200, DistanceUnit.KILOMETERS);

    SearchResponse response = client.prepareSearch("car_shop")

    .setTypes("shops")

    .setQuery(qb)

    .get();

    整体代码如下GeoLocationShopSearchApp

    
    public class GeoLocationShopSearchApp {
    
        @SuppressWarnings({ "unchecked", "resource" })
        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<GeoPoint>();             
            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("====================================================");
            
            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();
        }
        
    }
    
    
    

    相关文章

      网友评论

        本文标题:90_熟练掌握ES Java API_基于地理位置对周围汽车4S

        本文链接:https://www.haomeiwen.com/subject/cngilhtx.html