閱讀本文需先了解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");// 坐標點類型
}
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();
}