閱讀本文需先了解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);
}
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();
}