您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
elasticSearch地理查询-映射简化
发布时间:2021-09-07 22:32:00编辑:雪饮阅读()
就上篇末尾中我有讲到elasticSearch有些老的文档上面对地理查询的那个语法在我elasticsearch-7.14.0-windows-x86_64上面会出错。其实这里澄清下。没有的事情。
因为当时测试时候映射没有弄对。
但是那个老的文档上面有些地方是真的不太对,就是查询的时候。因为它那个经纬度的数据格式是用[]这种中括号的,实际上应该用lat和lon。
那么这里将映射简化的版本的整个流程整理下。
上篇中那种是索引及映射是一次性创建的,而我这里的情况是已经存在索引。
那么已经存在索引的情况下你可以像是下面这样更新索引的映射让其支持geo_point。
那么简化版映射更新为支持geo_point字段类型的方式如:
请求正文:
{
"properties": {
"name": {
"type": "text",
"fielddata": true
},
"ip": {
"type": "text",
"fielddata": true
},
"location": {
"type": "geo_point"
}
}
}
响应正文:
{
"acknowledged": true
}
那么上面这样的操作,如果说你这个索引下面已经存在有文档,且文档中也存在如location这个字段,那么可能会冲突,建议的是你这个索引下文档清空再试。
或者把那个字段修改下。
那么接下来就是创建拥有经纬度字段(geo_point类型)的文档
请求正文:
{
"name":"pune", "description":"ICSE", "street":"West End",
"state":"UP", "zip":"250002", "location":{"lat":28.9926174,"lon":77.692485}, "fees":3500,
"tags":["fully computerized"], "rating":"4.5"
}
响应正文:
{
"_index": "schools",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 3
}
可以看到这里的变化,之前location用[],现在要用{}并且囊括lat和lon,那么这里这种比上篇中那种就结构简化多了。只是相对于老的文档稍微麻烦了点。
另外需要注意的是这里type,也就是_doc,类型别填错了,上篇中我说在我这个elasticSearch版本中不行,就是因为我把type填写成了school了。。。。尴尬,误导众人。
那最后就是地理查询了,这里地理查询中对于geo_point类型字段查询的表达式也就简化多了。
请求正文:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"location": {
"lat": 28,
"lon": 77
}
}
}
}
}
}
响应正文:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "schools",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"name": "pune",
"description": "ICSE",
"street": "West End",
"state": "UP",
"zip": "250002",
"location": {
"lat": 28.9926174,
"lon": 77.692485
},
"fees": 3500,
"tags": [
"fully computerized"
],
"rating": "4.5"
}
}
]
}
}
关键字词:elasticSearch,地理查询,简化,映射