? ? ? ? ?ES為用戶提供了基于地理位置的搜索功能。它主要支持兩種類型的地理查詢:一種是地理點(diǎn)(geo_point),即經(jīng)緯度查詢,另一種是地理形狀查詢(geo_shape),即支持點(diǎn),線,圓形和多邊形等查詢。
? ? ? ? 從實(shí)用性來說,地理點(diǎn)(即geo_point)數(shù)據(jù)類型的使用更多一些,對(duì)于geo_point字段類型的查詢方式有三種,分別為geo_distance查詢(圓形區(qū)域查詢),geo_bounding_box查詢(矩形區(qū)域查詢)和geo_polygon查詢(多邊形區(qū)域查詢)。
1.geo_distance圓形區(qū)域查詢
geo_distance需要指定一個(gè)坐標(biāo)點(diǎn),在指定該點(diǎn)距離的范圍后,ES可查詢到以該點(diǎn)為中心,距離為半徑的圓形區(qū)域的數(shù)據(jù)。
1.1 查詢的DSL
GET index_school/_search
{
"_source": [ // 只返回部分字段
"name",
"latitude",
"longitude",
"devideNo",
"time"
],
"query": {
"geo_distance":{
"distance": "5km", // 距離范圍(半徑)為5km
"location":{ //中心點(diǎn)經(jīng)緯度
"lat": "18.231472",
"lon": "109.502083"
}
}
}
}
1.2 java實(shí)現(xiàn)
SearchRequest request = new SearchRequest(tableName);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.filter(QueryBuilders.geoDistanceQuery("location") //指定索引字段
.distance(inputDTO.getDistance()) //距離中心點(diǎn)范圍(半徑)
.point(18.231472,109.502083)); //中心點(diǎn)
request.source().query(boolQuery).size(10000).trackTotalHits(true);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
2.geo_bounding_box矩形區(qū)域查詢
geo_bounding_box查詢提供的是矩形內(nèi)的搜索,需要提供左上角和右下角的頂點(diǎn)坐標(biāo)。文章來源:http://www.zghlxwxcb.cn/news/detail-528685.html
2.1 查詢的DSL
GET index_school/_search
{
"_source": [
"name",
"latitude",
"longitude",
"devideNo",
"time"
],
"query": {
"geo_bounding_box":{
"location":{
"top_left":{ //設(shè)置左上角頂點(diǎn)坐標(biāo)
"lat": "18.431472",
"lon": "109.502083"
},
"bottom_right":{ //設(shè)置右下角頂點(diǎn)坐標(biāo)
"lat": "18.231472",
"lon": "109.202083"
}
}
}
}
}
2.2 java實(shí)現(xiàn)
SearchRequest request = new SearchRequest(tableName);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.filter(QueryBuilders.geoBoundingBoxQuery("location") //指定索引字段
.setCorners(inputDTO.getYMax(), inputDTO.getXMin(), //構(gòu)造矩形
inputDTO.getYMin(), inputDTO.getXMax()));
request.source().query(boolQuery).size(10000).trackTotalHits(true);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
3.geo_polygon多邊形區(qū)域查詢
geo_polygon比geo_bounding_box提供的地理范圍功能更加靈活,它支持靈活多變的多邊形內(nèi)數(shù)據(jù)查詢,使用該查詢需要提供多邊形所有頂點(diǎn)的坐標(biāo)。文章來源地址http://www.zghlxwxcb.cn/news/detail-528685.html
3.1 查詢的DSL
GET index_school/_search
{
"query": {
"geo_polygon":{
"location":{
"points":[
{
"lat": "20.219935",
"lon": "109.700590"
},
{
"lat": "20.118963",
"lon": "109.865898"
},
{
"lat": "20.148887",
"lon": "110.1842848"
},
{
"lat": "20.355594",
"lon": "111.097193"
},
{
"lat": "20.295775",
"lon": "111.791273"
}
]
}
}
}
}
3.2 java實(shí)現(xiàn)
String AREA_POINTS =
"109.70059057645672,20.219935185668575," +
"109.86589885747735,20.11896383759739," +
"110.18428481460053,20.148887724639927," +
"111.09719394252089,20.355594505110506," +
"111.79127352756524,20.295775199436054," +
"111.44955008175484,19.864326194135216," +
"110.65937420024187,18.52295323361459," +
"109.76478316052932,17.989118493905913," +
"109.57997296428381,17.95228226006386";
SearchRequest request = new SearchRequest(tableName);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
List<GeoPoint> points = new ArrayList<>();
String[] strings = AREA_POINTS.split(",");
for (int i = 0; i < strings.length; i = i + 2) {
points.add(new GeoPoint(Double.valueOf(strings[i + 1]), Double.valueOf(strings[i])));
}
boolQuery.filter(QueryBuilders.geoPolygonQuery("location",points));
request.source().query(boolQuery).size(10000).trackTotalHits(true);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
到了這里,關(guān)于Elasticsearch 基于地理位置的搜索查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!