es6.2.4學(xué)習(xí)----java實現(xiàn)附近搜索(附近的人)

閱讀本文需先了解es對地理位置的處理。

本文講述java代碼實現(xiàn)搜索附近的人的功能

第一步:創(chuàng)建可存儲地理位置信息的索引:

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");// 坐標(biāo)點類型
                    }
                    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獲得經(jīng)緯度

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

將獲取到的經(jīng)緯度和原信息添加到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("杭州市拱墅區(qū)祥園路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();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容