es6.2.4学习----java实现附近搜索(附近的人)

2018-07-09  本文已影响0人  轻易流逝

阅读本文需先了解es对地理位置的处理。

本文讲述java代码实现搜索附近的人的功能

第一步:创建可存储地理位置信息的索引:

public static void createIndex() throws IOException {

        RestHighLevelClient clientLocal = new RestHighLevelClient(RestClient
                .builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));

        CreateIndexRequest req = new CreateIndexRequest("location-demo");
        XContentBuilder builder = XContentFactory.jsonBuilder();

        builder.startObject();
        {
            builder.startObject("doc");
            {
                builder.startObject("properties");
                {
                    builder.startObject("name");
                    {
                        builder.field("type", "text");
                    }
                    builder.endObject();
                    builder.startObject("address");
                    {
                        builder.field("type", "text");
                    }
                    builder.endObject();
                    builder.startObject("location");
                    {
                        builder.field("type", "geo_point");// 坐标点类型
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();

        req.mapping("doc", builder);

        clientLocal.indices().create(req);
        clientLocal.close();
    }

也可直接手动新建

PUT /location-demo
{
  "mappings": {
      "doc": {
        "properties": {
          "address": {
            "type": "text"
          },
          "location": {
            "type": "geo_point"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
}

第二步:添加文档到索引中

public static void saveData() throws IOException {
        String[] names = { "小明", "小红", "小胖" };
        String[] addresses = { "杭州市汇和城购物中心", "杭州长运公路汽车站", "杭州市火车东站" };

        saveToEs(names, addresses);
    }

由于输入的是地址信息,所以我使用高德地图的api获得经纬度

public static void saveToEs(String[] names, String[] addresses) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient
                .builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));

        for (int i = 0; i < names.length; i++) {

            GeoPoint point = GDGeoUtil.getGeoPoint(city, addresses[i]);

            indexDoc(client, names[i], addresses[i], point);
        }

        client.close();
    }

将获取到的经纬度和原信息添加到es中

public static void indexDoc(RestHighLevelClient client, String name, String address, GeoPoint point)
            throws IOException {
        IndexRequest req = new IndexRequest("location-demo", "doc");

        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("name", name);
        jsonMap.put("address", address);
        String lat_lon = point.getLatitude() + "," + point.getLongitude();
        jsonMap.put("location", lat_lon);

        req.source(jsonMap);

        IndexResponse resp = client.index(req);
        System.out.println(resp);
    }

第三步:搜索附近的人

public static void main(String[] args) throws IOException {
        // createIndex();

        // saveData();

        search("杭州市拱墅区祥园路28号", 2);

    }
/**
     * @param addr
     * @param distance
     *            距离(千米)
     * @throws IOException
     */
    public static void search(String addr, int distance) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient
                .builder(new HttpHost("192.168.16.21", 9200, "http"), new HttpHost("192.168.16.22", 9200, "http")));

        GeoPoint point = GDGeoUtil.getGeoPoint(city, addr);

        SearchRequest req = new SearchRequest("location-demo");
        SearchSourceBuilder ssb = new SearchSourceBuilder();
        GeoDistanceQueryBuilder geoDistanceQueryBuilder = new GeoDistanceQueryBuilder("location");
        geoDistanceQueryBuilder.point(point.getLat(), point.getLon()).distance(distance,
                DistanceUnit.KILOMETERS);
        ssb.query(geoDistanceQueryBuilder);
        req.source(ssb);

        SearchResponse resp = client.search(req);
        System.out.println(resp);

        System.out.println("以“" + addr + "”为中心,周围" + distance + "公里的用户有:");
        SearchHits hits = resp.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        client.close();
    }
上一篇 下一篇

猜你喜欢

热点阅读