Elasticsearch 入门实战(8)--REST API 使用二(Search API)

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

本文继承上文(Elasticsearch 入门实战(3)--REST API 使用一(CAT,Index,Document,Ingest API))先容 Elasticsearch REST API,相关的情况及软件信息如下:CentOS 7.6.1810、Elasticsearch 8.13.4。
1、Search APIs

1.1、Count API(查询文档数目)

语法:
  1. GET /<target>/_count
复制代码
样例:
  1. curl -X GET 'http://10.49.196.33:9200/poet-index/_count'             #查询该索引的所有文档数量
  2. curl -X GET 'http://10.49.196.33:9200/poet-index/_count?q=name:杜甫' #通过 Lucene 查询语法指定条件
  3. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.33:9200/poet-index/_count' -d ' #通过 "Query DSL" 指定条件
  4. {
  5.   "query": {
  6.     "term": {
  7.       "name.keyword": {
  8.         "value": "杜甫"
  9.       }
  10.     }
  11.   }
  12. }'
复制代码
1.2、Search API(查询文档)

语法:
  1. GET /<target>/_search
  2. GET /_search
  3. POST /<target>/_search
  4. POST /_search
复制代码
1.2.1、query

1.2.1.1、term/terms 查询

term 查询不会对输入的内容进行分词处理,而是作为一个团体来查询。
A、查询单个词
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "term": {
  5.       "name": {
  6.         "value": "李白"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
B、查询多个词
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "terms": {
  5.       "name": ["李白", "杜甫"]
  6.     }
  7.   }
  8. }'
复制代码
1.2.1.2、range 查询

按照范围查询。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "range": {
  5.       "age": {
  6.         "gte": 20,
  7.         "lte": 35
  8.       }
  9.     }
  10.   }
  11. }'
复制代码
1.2.1.3、exists 查询

查询对应字段不为空的数据。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.1.101.64:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "exists": {
  5.       "field": "poems"
  6.     }
  7.   }
  8. }'
复制代码
1.2.1.4、match 相关查询

A、match
对输入的内容进行分词处理,再根据分词查询。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.       "success": "理想主义"
  6.     }
  7.   },
  8.   "from": 0,
  9.   "size": 10,
  10.   "sort": [{
  11.     "name": {
  12.       "order": "asc"
  13.     }
  14.   }]
  15. }'
复制代码
B、multi_match
多字段进行匹配。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "multi_match": {
  5.       "query": "太白",
  6.       "fields": ["about", "success"]
  7.     }
  8.   }
  9. }'
复制代码
C、match_phrase
类似 match,必要满足以下条件:
1.文档的分词列表要包含所有的搜索分词列表
2.搜索分词次序要和文档分词次序一致
3.slop 参数控制着匹配到的文档分词最大间距,默以为1(匹配到分词要紧挨着)
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match_phrase": {
  5.       "success": "文学作家"
  6.     }
  7.   }
  8. }'
复制代码
D、match_all
查询所有文档。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match_all": {
  5.      }
  6.   }
  7. }'
复制代码
不加请求体,也是一样的效果,查询所有文档。
  1. curl -X GET 'http://10.49.196.11:9200/poet-index/_search'
复制代码
E、match_none
与 match_all 相反,返回 0 个文档。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match_none": {
  5.      }
  6.   }
  7. }'
复制代码
1.2.1.5、query_string 查询

query_string 可以同时实现前面几种查询方法。
A、类似 match
  1. curl -X GET  -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": "古典文学"
  7.     }
  8.   }
  9. }'
复制代码
B、类似 mulit_match
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "query": "古典文学",
  6.       "fields": ["about", "success"]
  7.     }
  8.   }
  9. }'
复制代码
C、类似 match_phrase
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": ""古典文学""
  7.     }
  8.   }
  9. }'
复制代码
D、带运算符查询,运算符两边的词不再分词
1、查询同时包含 ”文学“ 和 ”巨大“ 的文档
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "default_field": "success",
  6.       "query": "文学 AND 伟大"
  7.     }
  8.   }
  9. }'
复制代码
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "fields": ["success"],
  6.       "query": "文学 伟大",
  7.       "default_operator": "AND"
  8.     }
  9.   }
  10. }'
复制代码
2、查询 name 或 success 字段包含"文学"和"巨大"这两个单词,或者包含"李白"这个单词的文档。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "query_string": {
  5.       "query": "(文学 AND 伟大) OR 李白",
  6.       "fields": ["name", "success"]
  7.     }
  8.   }
  9. }'
复制代码
1.2.1.6、simple_query_string 查询

类似 query_string,重要区别如下:
1、不支持AND OR NOT ,会当做字符处理;使用 + 代替 AND,| 代替OR,- 代替 NOT
2、会忽略错误的语法
查询同时包含 ”文学“ 和 ”巨大“ 的文档:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "simple_query_string": {
  5.       "fields": ["success"],
  6.       "query": "文学 + 伟大"
  7.     }
  8.   }
  9. }'
复制代码
1.2.1.7、fuzzy 查询

模糊查询时使用的参数:
fuzziness
允许的最大编辑间隔,默认不开启模糊查询,相称于 fuzziness=0。支持的格式
1、可以是数字(0、1、2)代表固定的最大编辑间隔
2、主动模式,AUTO:[low],[high]
    查询词长度在 [0-low)范围内编辑间隔为 0(即强匹配)
    查询词长度在 [low, high) 范围内允许编辑 1 次
    查询词长度 >high 允许编辑 2 次
prefix_length
控制两个字符串匹配的最小相同的前缀巨细,也就是前 n 个字符不允许编辑,必须与查询词相同,默认是 0,大于 0 时可以明显提升查询性能
max_expansions
产生的最大模糊选项
transpositions
相邻位置字符交换是否算作 1 次编辑间隔,全文查询不支持该参数
A、全文查询时使用模糊参数
先分词再计算模糊选项。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.        "success": {
  6.          "query": "古典文化",
  7.          "fuzziness": 1,
  8.          "prefix_length": 0,
  9.          "max_expansions": 5
  10.        }
  11.     }
  12.   }
  13. }'
复制代码
B、使用 fuzzy query
对输入不分词,直接计算模糊选项。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "fuzzy": {
  5.       "success": {
  6.         "value": "理想",
  7.         "fuzziness": 1,
  8.         "prefix_length": 0,
  9.         "transpositions": true
  10.       }
  11.     }
  12.   }
  13. }'
复制代码
1.2.1.8、wildcard 查询

wildcard 查询类似 SQL 语句中的 like;? 匹配一个字符,* 匹配多个字符。对于使用 wildcard 查询的字段发起字段类型设为 wildcard 类型。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "wildcard": {
  5.       "name": "李*"
  6.     }
  7.   }
  8. }'
复制代码
1.2.1.9、bool 查询

组合查询使用 bool 来组合多个查询条件。
条件阐明
must同时满足
should满足此中任意一个
must_not同时不满足
filter过滤搜索,不计算得分
A、查询 success 包含 “思想” 且 age 在 [20-40] 之间的文档:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [{
  6.         "simple_query_string": {
  7.           "query": "思想",
  8.           "fields": ["success"]
  9.         }
  10.       }, {
  11.         "range": {
  12.           "age": {
  13.             "gte": 20,
  14.             "lte": 40
  15.           }
  16.         }
  17.       }]
  18.     }
  19.   }
  20. }'
复制代码
B、过滤出 success 包含 “思想” 且 age 在 [20-40] 之间的文档,不计算得分:
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [{
  6.         "simple_query_string": {
  7.           "query": "思想",
  8.           "fields": ["success"]
  9.         }
  10.       }, {
  11.         "range": {
  12.           "age": {
  13.             "gte": 20,
  14.             "lte": 40
  15.           }
  16.         }
  17.       }]
  18.     }
  19.   }
  20. }'
复制代码
1.2.2、aggs 查询

聚合查询类似 SQL 中的 group by 分组查询。
A、求和,类似 select sum(age) from poet-index
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_sum": {
  5.       "sum": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
B、类似 select count distinct(age) from poet-index
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/test-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_count": {
  5.       "cardinality": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   }
  10. }'
复制代码
C、数目、最大、最小、平均、求和
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "age_stats": {
  5.       "stats": {
  6.         "field": "age"
  7.       }
  8.     }
  9.   },
  10.   "size": 0
  11. }'
复制代码
D、类似 select name,count(*) from poet-index group by name
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "name_terms": {
  5.       "terms": {
  6.         "field": "name"
  7.       }
  8.     }
  9.   },
  10.   "size": 0
  11. }'
复制代码
E、类似 select name,age, count(*) from poet-index group by name,age
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "aggs": {
  4.     "name_terms": {
  5.       "terms": {
  6.         "field": "name"
  7.       },
  8.       "aggs": {
  9.         "age_terms": {
  10.           "terms": {
  11.             "field": "age"
  12.           }
  13.         }
  14.       }
  15.     }
  16.   },
  17.   "size": 0
  18. }'
复制代码
F、类似 select avg(age) from poet-indexwhere name='李白'
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": {
  6.         "term": {
  7.           "name": "李白"
  8.         }
  9.       }
  10.     }
  11.   },
  12.   "aggs": {
  13.     "age_avg": {
  14.       "avg": {
  15.         "field": "age"
  16.       }
  17.     }
  18.   },
  19.   "size": 0
  20. }'
复制代码
1.2.3、suggest 查询

如果希望 Elasticsearch 能够根据我们的搜索内容给一些保举的搜索选项,可以使用保举搜索。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "suggest": {
  4.     "success_suggest": {
  5.       "text": "思考",
  6.       "term": {
  7.         "field": "success",
  8.         "analyzer": "ik_max_word",
  9.         "suggest_mode": "always",
  10.         "min_word_length":2
  11.       }
  12.     }
  13.   }
  14. }'
复制代码
保举模式 suggest_mode:
 保举模式阐明
popular保举词频更高的一些搜索
missing当没有要搜索的结果的时间才保举
always无论什么情况下都进行保举
1.2.4、highlight

对搜索结果中的关键字高亮显示。
  1. curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
  2. {
  3.   "query": {
  4.     "match": {
  5.       "success": "思想"
  6.     }
  7.   },
  8.   "highlight": {
  9.     "pre_tags": "<span color='red'>",  
  10.     "post_tags": "</span>",        
  11.     "fields": {            
  12.       "success": {}
  13.     }
  14.   }
  15. }'
复制代码
 

 
详细的 Elasticsearch REST API 使用阐明,请参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表