elasticsearch经纬度范围查询
2018-05-09 20:55:49
1148次阅读
0个评论
索引mapping定义:
索引中定义一个字段pin,添加一个属性location,type为geo_point
DSL:
报文中的包含一个match all的query , filter中的distance指定了距离范围,pin.location是经纬度
拼装 query 和 sort
索引中定义一个字段pin,添加一个属性location,type为geo_point
"pin" : {
"properties" : {
"location" : {
"type" : "geo_point"
}
}
}
DSL:
报文中的包含一个match all的query , filter中的distance指定了距离范围,pin.location是经纬度
{
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "200km",
"pin.location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
拼装 query 和 sort
QueryBuilder builder = new GeoDistanceRangeQueryBuilder ("pin.location")
.point(lat,lon)
.from("0km")
.to("10000km")
.includeLower(true)
.includeUpper(false)
.optimizeBbox("memory")
.geoDistance(GeoDistance.ARC);
GeoDistanceSortBuilder sort = SortBuilders.geoDistanceSort("location");
GeoDistanceSortBuilder sort = new GeoDistanceSortBuilder("location");
sort.unit(DistanceUnit.KILOMETERS);
sort.order(SortOrder.ASC);
sort.point(lat,lon);
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
sourceBuilder.query(QueryBuilders.boolQuery())
.must(builder)
sourceBuilder.sort(sort);
final SearchResponse response = executeGet(new ClientCallback<SearchResponse>() {
@Override
public ActionFuture<SearchResponse> execute(final Client client) {
final SearchSourceBuilder sourceBuilder = SearchParamUtils
.genSearchSourceBuilderFromSearchParam(searchParam);
String[] indexNames = new String[aliasIndexNameList.size()];
SearchRequest request = Requests.searchRequest(aliasIndexNameList.toArray(indexNames))
.types(type);
request.source(sourceBuilder.toString());
if (searchParam.getSearchType() != null) {
request.searchType(searchParam.getSearchType());
}
return client.search(request);
}
});
SearchResult searchResult = new SearchResult();
SearchHits hits = response.getHits();
for (SearchHit hit : hits.getHits()) {
Object[] sortArray = hit.getSortValues();
if(sortArray!=null&&sortArray.length>0){
BigDecimal geoDis = new BigDecimal((Double) sortArray[sortArray.length-1]);
map.put("geoDistance", geoDis.setScale(0, BigDecimal.ROUND_HALF_DOWN));
System.out.println("距离" + hit.getSource().get("geoDistance"));
}
}
00
相关话题
- Elasticsearch 6.3.0 SQL查询
- 生产环境 SQL查询优化
- Elasticsearch score 评分计算
- elasticsearch清空索引缓存
- Elasticsearch使用索引别名
- Elasticsearch Aggregation聚合统计
- Elasticsearch多字段聚合
- ElasticSearch 6.0 乐观锁
- Elasticsearch 5.0 深分页
- Elasticsearch 6.0 性能调优
- Elasticsearch使用searchAfter深度分页
- ElasticSearch 6.0 Bulk批量操作
- ElasticSearch 6.0 搜索结果高亮
- ElasticSearch6.0配置IK分词插件
- Elasticsearch 不停止服务更新索引别名