ES的地圖檢索方式
ES支持的地圖檢索方式有以下幾種;
geo_distance
geo_bounding_box
geo_polygon
1、geo_distance:直線距離檢索,如給定點(diǎn)A,要求返回地圖上距離點(diǎn)A三千米的商家(點(diǎn)外賣場景)
2、查找索引內(nèi)距離北京站(116.433733,39.908404)3000米內(nèi)的點(diǎn)
geo_distance涉及的參數(shù)如下
location:確定一個點(diǎn);
distance:確定一個半徑,單位米
distance_type:確定一個圖形的類型;一般是圓形,arc
POST /map/_search
{
"query": {
"geo_distance":
{
"location":
{
"lon":116.433733
,"lat":39.908404
},
"distance":3000,
"distance_type":"arc"
}
}
}
創(chuàng)建geo_point類型字段映射:
PUT test?
{
? "mappings": {
? ? "user": {
? ? ? "properties": {
? ? ? ? "location": {
? ? ? ? ? "type": "geo_point"
? ? ? ? }
? ? ? }
? ? }
? }
}
加入依賴:
使用spring-data-elasticsearch依賴
<!-- ElasticSearch -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
? ? ? ? </dependency>
?3、創(chuàng)建Doc文檔對象:
package cn.nagisa.geo.doc;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
/**
?* @author nagisa
?*/
@Data
@Document(indexName = "test",type = "user")
public class UserDoc {
? ? private Long id;
? ? private String username;
? ? @GeoPointField
? ? private GeoPoint location;
}
這里的Doc相當(dāng)于entity,注意加上@GeoPointField,表示localcation是Es當(dāng)中g(shù)eo_point類型的字段
/**
* @param lat 區(qū)域中心的緯度
* @param lng 區(qū)域中心經(jīng)度
* @param distance 區(qū)域半徑
* @return 符合條件的數(shù)據(jù)
*/
@Override
public JsonResult fixedArea(Double lat, Double lng, Integer distance) {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// 以某點(diǎn)為中心,搜索指定范圍
GeoDistanceQueryBuilder distanceQueryBuilder = new GeoDistanceQueryBuilder("location");
distanceQueryBuilder
.point(lat, lng)
.distance(distance, DistanceUnit.KILOMETERS);
boolQueryBuilder.filter(distanceQueryBuilder);
//查詢封裝
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
NativeSearchQuery build = nativeSearchQueryBuilder
.withQuery(boolQueryBuilder)
.build();
return JsonResult.me().setResult(userRepository.search(build));
}
錯誤排查:
Error: all shards failed
可能原因:經(jīng)緯度調(diào)換,傳反了
ES--經(jīng)緯度查詢_es 經(jīng)緯度查詢_寶哥大數(shù)據(jù)的博客-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-624673.html
SpringBoot+ElasticSearch根據(jù)經(jīng)緯度,簡單搜索指定距離范圍內(nèi)的數(shù)據(jù)_springboot經(jīng)緯度在5公里內(nèi)_Nagisa-的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-624673.html
到了這里,關(guān)于ElasticSearch - 根據(jù)經(jīng)緯度,簡單搜索指定距離范圍內(nèi)的數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!