Elasticsearch地理位置维护及检索

bboss

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

1.准备工作

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

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

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

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

  1. <properties>
  2. <property name="createCityIndice"><![CDATA[{
  3. "settings": {
  4. "number_of_shards": 6,
  5. "index.refresh_interval": "5s"
  6. },
  7. "mappings": {
  8. "city": {
  9. "properties": {
  10. "standardAddrId":{
  11. "type":"keyword"
  12. },
  13. "detailName": {
  14. "type": "text",
  15. "fields": {
  16. "keyword": {
  17. "type": "keyword"
  18. }
  19. }
  20. },
  21. "cityName":{
  22. "type": "text",
  23. "fields": {
  24. "keyword": {
  25. "type": "keyword"
  26. }
  27. }
  28. },
  29. "countyName":{
  30. "type": "text",
  31. "fields": {
  32. "keyword": {
  33. "type": "keyword"
  34. }
  35. }
  36. },
  37. "location":{
  38. "type": "geo_point"
  39. }
  40. }
  41. }
  42. }
  43. }]]></property>
  44. <property name="locationSearch"><![CDATA[{
  45. "size": 100,
  46. "sort": [
  47. {
  48. "_geo_distance": { ##按离指定地理坐标对应的地理位置远近距离升序排序
  49. "unit": "km",
  50. "order": "asc",
  51. "location": { ##指定参考地理坐标位置
  52. "lon": #[lon],
  53. "lat": #[lat]
  54. }
  55. }
  56. },
  57. {
  58. "totalSaleNum": {
  59. "order": "desc"
  60. }
  61. }
  62. ],
  63. "query": {
  64. "bool": {
  65. "must": [
  66. {
  67. "match_phrase_prefix" : {
  68. "detailName" : {
  69. "query" : #[detailName]
  70. }
  71. }
  72. },
  73. {
  74. "geo_distance": {
  75. "distance": #[distance],
  76. "location": {
  77. "lon": #[lon],
  78. "lat": #[lat]
  79. }
  80. }
  81. }
  82. ]
  83. }
  84. }
  85. }]]></property>
  86. </properties>

创建索引表

  1. //创建加载配置文件的客户端工具,单实例多线程安全,第一次运行要预加载,有点慢
  2. ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/address.xml");
  3. try {
  4. //先删除名称为city的mapping
  5. clientUtil.dropIndice("city");
  6. } catch (ElasticSearchException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. //再创建mapping
  11. clientUtil.createIndiceMapping("city",//索引表名称
  12. "createCityIndice");//索引表mapping dsl脚本名称,在esmapper/address.xml中定义createCityIndice
  13. String mapping = clientUtil.getIndice("city");//获取刚才创建的索引结构
  14. System.out.println(mapping);

3.添加索引文档

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

4.地理位置检索

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