欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-31 08:41:15
...
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
6.开发交流
bboss elasticsearch技术交流群:166471282

bboss elasticsearch微信公众号:
Elasticsearch地理位置维护及检索案例分享
            
    
    博客分类: bboss elastic elasticsearchbboss 
相关标签: elasticsearch bboss