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

阅读本文需先了解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 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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值