Elasticsearch地理位置维护及检索案例分享


声明:本文转载自https://my.oschina.net/bboss/blog/1788008,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

Elasticsearch地理位置信息维护及检索案例分享

1.准备工作

参考文档《高性能elasticsearch ORM开发库使用介绍》导入和配置es客户端到工程

2.定义带地理位置类型的mapping

创建一个city索引表结构,字段location的 类型为geo_point,并且定义一个检索的dsl语句

在resources目录下创建文件esmapper/address.xml,内容如下:

<properties> <property name="createCityIndice"><![CDATA[{   "settings": {          "number_of_shards": 6,          "index.refresh_interval": "5s"   },   "mappings": {                 "city": {                     "properties": {                         "standardAddrId":{                             "type":"keyword"                         },                         "detailName": {                             "type": "text",                                                           "fields": {                                 "keyword": {                                     "type": "keyword"                                 }                             }                         },                         "cityName":{                             "type": "text",                             "fields": {                                 "keyword": {                                     "type": "keyword"                                 }                             }                         },                         "countyName":{                             "type": "text",                             "fields": {                                 "keyword": {                                     "type": "keyword"                                 }                             }                         },                         "location":{                             "type": "geo_point"                         }                      }                 }             } }]]></property> <property name="locationSearch"><![CDATA[{         "size": 100,         "query": {             "bool": {                 "must": [                     {                           "match_phrase_prefix" : {                                 "detailName" : {                                     "query" : #[detailName]                                 }                             }                      },                     {                         "geo_distance": {                             "distance": #[distance],                             "location": {                                 "lon": #[lon],                                 "lat": #[lat]                             }                         }                     }                 ]             }         }     }]]></property> </properties>

创建索引表

//创建加载配置文件的客户端工具,单实例多线程安全,第一次运行要预加载,有点慢 		ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/address.xml"); 		try { 			//先删除mapping 			clientUtil.dropIndice("city"); 		} catch (ElasticSearchException e) { 			// TODO Auto-generated catch block 			e.printStackTrace(); 		} 		//再创建mapping 		clientUtil.createIndiceMapping("city",//索引表名称 				"createCityIndice");//索引表mapping dsl脚本名称,在esmapper/address.xml中定义createCityIndice

3.添加索引文档

Map<String,String> params = new HashMap<String,String>(); 		params.put("cityName","潭市"); 		params.put("standardAddrId","38130122"); 		params.put("detailName","贵溪市花园办事处建设路四冶生活区4-11栋33单元1层1010"); 		params.put("location","28.292781,117.238963"); 		params.put("countyName","中国"); ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();				 clientUtil.addDocument("city",//索引名称                        "city",//索引类型                         params);//索引数据对象                         "refresh");//强制刷新索引数据,让插入数据实时生效,如果考虑性能需要,可以去掉refresh参数

4.地理位置检索 

ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/address.xml"); 		Map<String,String> params = new HashMap<String,String>(); 		params.put("detailName","海域香廷160栋1单元3层302室"); 		params.put("distance","0.5km"); 		params.put("lon","115.824994"); 		params.put("lat","28.666162"); //返回map对象列表,也可以返回其他实体对象列表 		ESDatas<Map> datas = clientUtil.searchList("city/_search","locationSearch",params,Map.class); //返回json报文 		System.out.print(clientUtil.executeRequest("city/_search","locationSearch",params));

5.参考文档

更多bboss 使用文档可以参考:

https://my.oschina.net/bboss/blog/1556866

本文发表于2018年04月01日 22:38
(c)注:本文转载自https://my.oschina.net/bboss/blog/1788008,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1850 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1