Java根据坐标查区域_Elasticsearch 之(53) Java API 基于geo_shape根据坐标查找 坐标落在店铺范围的店铺...

根据坐标查找 坐标落在店铺范围的店铺

构建mapping

PUT /example

{

"mappings": {

"doc": {

"properties": {

"location": {

"type": "geo_shape"

}

}

}

}

}初始化数据

POST /example/doc

{

"location" : {

"type" : "polygon",

"coordinates" : [

[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]

]

}

}

上例中大量的方括号可能看起来让人困惑,不过实际上 GeoJSON 的语法非常简单:

用一个数组表示 经纬度 坐标点:[lon,lat]

一组坐标点放到一个数组来表示一个多边形:[[lon,lat],[lon,lat], ... ]

一个多边形( polygon )形状可以包含多个多边形;第一个表示多边形的外轮廓,后续的多边形表示第一个多边形内部的空洞:[

[[lon,lat],[lon,lat], ... ], # main polygon

[[lon,lat],[lon,lat], ... ], # hole in main polygon

...

]

public class GeoLocationShopSearchApp {

@SuppressWarnings({ "resource", "unchecked" })

public static void main(String[] args) throws Exception {

Settings settings = Settings.builder()

.put("cluster.name", "elasticsearch")

.build();

TransportClient client = new PreBuiltTransportClient(settings)

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

;

Coordinate coordinate = new Coordinate(100.3, 0.3);

GeoShapeQueryBuilder qb = null;

try {

qb = geoShapeQuery(

"0",

ShapeBuilders.newPoint(coordinate));

} catch (IOException e) {

e.printStackTrace();

}

qb.relation(ShapeRelation.INTERSECTS);

SearchResponse response = client.prepareSearch("example")

.setTypes("doc")

.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)

.setQuery(qb)

.setSize(200)

.setFrom(0)

.execute()

.actionGet();

if (response != null && response.getHits() != null && response.getHits().getHits()!=null) {

final ArrayList searchHits = Lists.newArrayList(response.getHits().getHits());

searchHits.stream().forEach(searchHit -> printHitsSource(searchHit));

}

client.close();

}

public static void printHitsSource(SearchHit searchHitFields){

Map source = searchHitFields.getSource();

System.out.println(new Gson().toJson(source));

}

}

{"location":{"coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]],"type":"polygon"}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值