Elasticsearch系列(14)Query之地理空间查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch系列(14)Query之地理空间查询相关的知识,希望对你有一定的参考价值。

参考技术A Elasticsearch支持两种类型的地理数据:支持经纬度对的geo_point字段和支持点、线、圆、多边形等的geo_shape字段。接下来介绍Elasticsearch提供的地理相关的查询,包括:

geo_bounding_box查询查找地理点位于指定矩形中的文档。
首先,创建一个地理索引文档。

使用geo_bounding_box查询示例如下:

top_left:矩形的左上角位置,值为geo_point类型。
bottom_right:矩形的右下角位置,值为geo_point类型。

geo_point类型可以接受不同输入格式的地理点。

查找地理点位于一个中心点到指定距离范围内的文档。
使用geo_distance查询,中心地理坐标为"lat" : 40, "lon" : -70,查询半径distance为200km,示例如下:

以下是过滤器上的查询参数:

查找地理点位于指定多边形的文档。
使用geo_polygon查询,设置三个坐标地理点,形成一个三角形,示例如下:

以下是过滤器上的查询参数:

查找与指定的地理形状相交、包含或不相交的地理形状的文档。使用geo_shape类型来过滤索引文档。
geo_shape查询支持两种定义查询形状的方法,一种是提供完整的形状定义,另一种是引用另一个索引中预先索引形状的名称。

与geo_shape类型类似,geo_shape查询使用GeoJSON表示形状。
首先给定以下索引:

使用Elasticsearch的envelope模式,GeoJSON形式来查询地理点,示例如下:

可以使用Kibana中的Maps功能查看查询结果如下所示:

首先有一个预定义的形状列表,并且指定一个逻辑名称来引用它,这样避免了每次都必须提供它们的坐标。在预先索引形状的情况下,只需要提供:

首先预先定义索引形状,示例如下:

根据预先索引形状来查询,示例如下:

geo_shape查询通过设置relation参数来决定在搜索时可以使用哪些空间关系(relation)操作符,空间关系(relation)的完整列表如下:
INTERSECTS - (默认值)返回geo_shape字段与查询几何图形相交的所有文档。
DISJOINT - 返回geo_shape字段与查询几何形状没有任何共同之处的所有文档。
WITHIN - 返回geo_shape字段在查询几何图形内的所有文档。
CONTAINS - 返回geo_shape字段包含查询几何图形的所有文档。

Elasticsearch语法知多少之Match query

目录

目标

ES版本信息

官方文档

相关术语

创建相关的索引和文档(数据用于实战案例)

创建索引

索引文档

Match query常见参数实战

基本语法

analyzer(指定分词器查询)

operator(解释查询条件的布尔逻辑)

minimum_should_match(最少匹配数)

fuzzy(模糊搜索)


目标

掌握匹配查询,本文会列举各种常见的案例,通过这些案例来熟悉匹配查询各个参数的功能和使用方法。


ES版本信息

7.17.5


官方文档

Match queryhttps://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl.html


相关术语

Match query

即匹配查询。返回与提供的文本、数字、日期或布尔值匹配的文档。在匹配之前分析提供的文本。匹配查询是执行全文搜索的标准查询,包括模糊匹配选项。


创建相关的索引和文档(数据用于实战案例)

创建索引

PUT /student_db

  "settings": 
    "index": 
      "analysis.analyzer.default.type": "ik_max_word"
    
  


PUT /address_list

  "mappings": 
    "properties": 
      "province": 
        "type": "text",
        "copy_to": "fullAddress"
      ,
      "city": 
        "type": "text",
        "copy_to": "fullAddress"
      ,
      "county": 
        "type": "text",
        "copy_to": "fullAddress"
      
    
  ,
  "settings": 
    "index": 
      "analysis.analyzer.default.type": "ik_max_word"
    
  

索引文档

PUT /student_db/_bulk
"index":"_id":"1"
"province":"湖南省","city":"长沙市","county":"天心区","describe":"侠客岛服务员A。","stu_id":"10001","stu_name":"张三","age":10,"sex":true,"birthday":"2000-01-01","hobby":["唱歌","跳舞","篮球"],"examination_results":"Math":"value":98.5,"level":"优","English":"value":87.5,"level":"良"
"index":"_id":"2"
"province":"湖南省","city":"长沙市","county":"芙蓉区","describe":"侠客岛服务员B。","stu_id":"10002","stu_name":"李四","age":12,"sex":true,"birthday":"1998-01-01","hobby":["唱歌","跳舞","游泳"],"examination_results":"English":"value":97.5,"level":"优","Chinese":"value":85.5,"level":"良"
"index":"_id":"3"
"province":"湖北省","city":"武汉市","county":"江夏区","describe":"会九阳神功、乾坤大挪移、圣火令武功、太极拳,太极剑等武功。","stu_id":"10003","stu_name":"张无忌","age":11,"sex":false,"birthday":"1999-01-01","hobby":["乒乓球","跳舞","游泳"],"examination_results":"Physics":"value":77.5,"level":"一般","Chinese":"value":100,"level":"优"
"index":"_id":"4"
"province":"湖北省","city":"黄石市","county":"铁山区","describe":"会黯然销魂掌、弹指神功、玉女剑法等武功。","stu_id":"10004","stu_name":"杨过","age":9,"sex":false,"birthday":"2001-01-01","hobby":["乒乓球","唱歌","游泳"],"examination_results":"Chemistry":"value":70.5,"level":"一般","Chinese":"value":91.5,"level":"优"
"index":"_id":"5"
"province":"广东省","city":"广州市","county":"南沙区","describe":"辽国南院大王,精通降龙十八掌,真正的战神。","stu_id":"10005","stu_name":"萧峰","age":13,"sex":true,"birthday":"1997-01-01","hobby":["篮球","足球","乒乓球"],"examination_results":"FineArts":"value":92.5,"level":"优","Sports":"value":91.5,"level":"优"
"index":"_id":"6"
"province":"广东省","city":"广州市","county":"南沙区","describe":"精通降龙十八掌,为国为民的侠之大者。","stu_id":"10006","stu_name":"郭靖","age":13,"sex":true,"birthday":"1997-01-01","hobby":["篮球","足球","乒乓球"],"examination_results":"History":"value":92.5,"level":"优","Chemistry":"value":91.5,"level":"优"
"index":"_id":"7"
"province":"广东省","city":"广州市","county":"白云区","describe":"会降龙十八掌,逍遥派诸多武功。","stu_id":"10007","stu_name":"虚竹","age":14,"sex":false,"birthday":"1996-01-01","hobby":["篮球","足球","乒乓球"],"examination_results":"History":"value":90.5,"level":"优","Chemistry":"value":94.5,"level":"优"
"index":"_id":"8"
"province":"广东省","city":"广州市","county":"白云区","describe":"会六脉神剑和北冥神功。","stu_id":"10008","stu_name":"段誉","age":14,"sex":false,"birthday":"1996-01-01","hobby":["篮球","足球","乒乓球"],"examination_results":"History":"value":90.5,"level":"优","Chemistry":"value":94.5,"level":"优"
"index":"_id":"9"
"province":"广东省","city":"广州市","county":"白云区","describe":"以光复大燕国为己任,会斗转星移和参合指。","stu_id":"10009","stu_name":"慕容复","age":15,"sex":false,"birthday":"1995-01-01","hobby":["篮球","游泳","乒乓球"],"examination_results":"History":"value":90.5,"level":"优","Chemistry":"value":94.5,"level":"优"
"index":"_id":"10"
"province":"广东省","city":"广州市","county":"白云区","describe":"斗转星移的创作者。","stu_id":"10010","stu_name":"慕容龙城","age":15,"sex":false,"birthday":"1995-01-01","hobby":["篮球","游泳","乒乓球"],"examination_results":"History":"value":90.5,"level":"优"
"index":"_id":"11"
"province":"北京市","city":"朝阳区","county":"三里屯街道","describe":"会少林七十二绝技,以佛法和慈悲度化慕容博和萧远山,是佛法和武功的集大成者。","stu_id":"10011","stu_name":"扫地僧","age":9,"sex":false,"birthday":"2001-01-01","hobby":["篮球","游泳","乒乓球"],"examination_results":"History":"value":100,"level":"优","Chinese":"value":100,"level":"优","Chemistry":"value":94.5,"level":"优","English":"value":100,"level":"优","Physics":"value":100,"level":"优","Math":"value":100,"level":"优"
"index":"_id":"12"
"province":"湖南省","city":"长沙市","county":"天心区","describe":"九阴真经的作者,武学创作天赋真正的第一人。","stu_id":"10012","stu_name":"黄裳","age":10,"sex":true,"birthday":"2000-01-01","hobby":["唱歌","跳舞","篮球"],"examination_results":"Math":"value":98.5,"level":"优","English":"value":87.5,"level":"良"
"index":"_id":"13"
"province":"湖南省","city":"长沙市","county":"天心区","describe":"根据九阴真经创作了九阳神功。","stu_id":"10013","stu_name":"斗酒僧","age":10,"sex":true,"birthday":"2000-01-01","hobby":["唱歌","跳舞","篮球"],"examination_results":"Math":"value":100,"level":"优","English":"value":100,"level":"优"
"index":"_id":"14"
"province":"湖南省","city":"长沙市","county":"天心区","describe":"绝技先天功,大器晚成,第一届华山论剑夺得九阴真经。","stu_id":"10014","stu_name":"王重阳","age":10,"sex":true,"birthday":"2000-01-01","hobby":["唱歌","跳舞","篮球"],"examination_results":"Math":"value":100,"level":"优","English":"value":100,"level":"优"

PUT /address_list/_bulk
 "index":  "_id": "1"   
"province": "湖南省","city": "长沙市","county":"天心区"
 "index":  "_id": "2"   
"province": "湖南省","city": "长沙市","county":"芙蓉区"
 "index":  "_id": "3"   
"province": "广东省","city": "广州市","county":"白云区"
 "index":  "_id": "4"   
"province": "湖北省","city": "武汉市","county":"江夏区"
 "index":  "_id": "4"   
"province": "内蒙古自治区","city": "呼和浩特","county":"玉泉区"

Match query常见参数实战

基本语法

需求:全文检索describe字段,匹配值为真经。

第一步:以ik分词器对真经分词,发现分词结果为:"真经"。

POST _analyze

  "analyzer": "ik_max_word",
  "text": "真经"

第二步:匹配查询。

#方法一
GET /student_db/_search

  "query": 
    "match": 
      "describe": "真经"
    
  


#方法二
GET /student_db/_search

  "query": 
    "match": 
      "describe": 
        "query": "真经"
      
    
  

analyzer(指定分词器查询)

需求:指定标准分词器全文检索describe字段,匹配值为真经。

第一步:以标准分词器对真经分词,发现分词结果为:"真","经"。

POST _analyze

  "analyzer": "standard",
  "text": "真经"

第二步:指定标准分词器匹配查询。

GET /student_db/_search

  "query": 
    "match": 
      "describe": 
        "query": "真经",
        "analyzer": "standard"
      
    
  

operator(解释查询条件的布尔逻辑)

需求:对省市县合并后的字段做匹配查询,查询条件是"湖南天心区"。用AND和OR演示该参数的使用方法。

第一步:以ik分词器对"湖南天心区"分词,发现分词结果为:"湖南","南天","天心区","天心","区"。

POST _analyze

  "analyzer": "ik_max_word",
  "text": "湖南天心区"

第二步:对于operator参数分别用AND和OR演来查询。发现用AND查不到数据,因为用ik分词器对fullAddress对应的全地址分词,发现没有一个地址分词以后同时拥有"湖南","南天","天心区","天心","区"。而用OR则可以查到数据,因为OR只要地址分词以后有一个分词在"湖南","南天","天心区","天心","区"就能匹配。注意:该参数默认值为OR。

GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "operator": "AND"
      
    
  


GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "operator": "OR"
      
    
  

minimum_should_match(最少匹配数)

需求一:对省市县合并后的字段做匹配查询,查询条件是"湖南天心区"。分别设置最少匹配数量为3、2、1,比较它们最终返回的结果。

第一步:以ik分词器对"湖南天心区"分词,发现分词结果为:"湖南","南天","天心区","天心","区"。这里我们可以说分词总数是5个,或者说子句数量是5个。

POST _analyze

  "analyzer": "ik_max_word",
  "text": "湖南天心区"

第二步:查询。发现数字越大,返回的数据越精准;数字越小,返回的数据越多。所以实际生产中需要合理配置该值。

GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "minimum_should_match":3
      
    
  


GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "minimum_should_match":2
      
    
  


GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "minimum_should_match":1
      
    
  

需求二:对省市县合并后的字段做匹配查询,查询条件是"湖南天心区"。分别设置最少匹配数量为1<60%、1<59%,比较它们最终返回的结果。

第一步:根据需求一得出以ik分词器对"湖南天心区"分词,发现分词结果为:"湖南","南天","天心区","天心","区"。

第二步:查询。发现minimum_should_match=1<60%查询1个文档,minimum_should_match=1<59%查询2个文档。因为子句数量为5,则既要满足minimum_should_match=1,又要满足minimum_should_match=百分比数。

GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "minimum_should_match":"1<60%"
      
    
  


GET /address_list/_search

  "query": 
    "match": 
      "fullAddress": 
        "query": "湖南天心区",
        "minimum_should_match":"1<59%"
      
    
  

附录

官方文档中描述了该参数可以拥有多种类型的值,比如按照分词数的百分比计算,这里列出了使用方法。

类型取值案例描述
正整数3分词数量至少匹配3个才符合条件。
负整数-2minimum_should_match=子句数量+这个负整数。该负数越小,查询到的数据越多。如果这个负整数太小,小于分词总数,则表示minimum_should_match=1。
正百分比75%符合子句数量的75%则匹配成功,比如子句数量是4,则需要至少有3个分词匹配,该文档才能被匹配;但是子句数量是5,则只需要3个匹配即可,即minimum_should_match=向下取整(子句数量X正百分比)。
负百分比-25%符合子句数量的(100%-25%)则匹配成功,比如子句数量是4,则需要至少有3个分词匹配,该文档才能被匹配;但是子句数量是5,则只需要4个匹配即可,即minimum_should_match=向上取整(子句数量X(100%+负百分比))。
组合1<60%见需求二的实现过程。
多种组合2<60% 9<-4用空格隔开,如果子句数量小于等于2,则每个组合都要匹配,如果数量为3到9个,则需要匹配60%,如果大于9,则需要匹配minimum_should_match=子句数量-4。

fuzzy(模糊搜索)

参数

fuzziness(编辑距离):输入的关键词通过几次操作可以转变为文档中对应的字段的值,这里的操作表示增删改以及相邻字符位置的交换。

#修改"制"变成"治",为1次。
内蒙古自制区->内蒙古自治区
#新增"治区",为2次。
内蒙古自->内蒙古自治区
#删除"区",为1次。
内蒙古自治区区->内蒙古自治区
#交换"治自"为"自治",为1次。
内蒙古治自区->内蒙古自治区

默认值为0表示不开启模糊搜索。为1表示允许一次修改,如:文档中字段的值为"内蒙古自治区",此时的搜索条件为"内蒙古古自治区"、"内蒙股自治区"、"内蒙自治区","内蒙古治自区"都可以搜索出该文档,因为搜索条件只经过了一次修改操作。要特别注意

  1. fuzzy最大为2;
  2. 搜索关键词长度=2,不允许存在模糊;
  3. 搜索关键词长度为3-5,允许1次模糊;
  4. 搜索关键词长度大于5,允许2次模糊。
  5. 官方推荐使用"AUTO",即根据情况自动设定。

prefix_length(前缀长度):模糊搜索时,要求搜索关键词的前缀必须匹配,这里的匹配长度由该参数控制。

需求一:输入关键词模糊搜索省份字段,通过控制编辑距离和前缀长度熟悉两个参数的使用方法。

GET /address_list/_search

  "query": 
    "fuzzy": 
      "province": 
        "value":"湖x省",
        "fuzziness": 1
      
    
  


GET /address_list/_search

  "query": 
    "fuzzy": 
      "province": 
        "value":"湖x省",
        "fuzziness": 1,
        "prefix_length":2
      
    
  


GET /address_list/_search

  "query": 
    "fuzzy": 
      "province": 
        "value":"内蒙自治区",
        "fuzziness": 1,
        "prefix_length":2
      
    
  


GET /address_list/_search

  "query": 
    "fuzzy": 
      "province": 
        "value":"内蒙古字智区",
        "fuzziness": 2,
        "prefix_length":2
      
    
  


GET /address_list/_search

  "query": 
    "fuzzy": 
      "province": 
        "value":"内蒙古治自区",
        "fuzziness": 1,
        "prefix_length":2
      
    
  

以上是关于Elasticsearch系列(14)Query之地理空间查询的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch系列(13)Query之全文查询

Elasticsearch Query DSL 整理总结—— Match Phrase Query 和 Match Phrase Prefix Query

Elasticsearch的javaAPI之query dsl-queries

Elasticsearch语法知多少之Match query

Elasticsearch Query DSL之全文检索(Full text queries)下篇

Elasticsearch Query DSL之全文检索(Full text queries)上篇