程序员小强总结的 ElasticSearch专题超全总结篇在这里:传送门
结合官网资料,做了更详细的实际使用总结。
从单机版安装到集群高可用生产环境搭建、基本概念(索引,分片,节点,倒排索引…)、DSL语法实践、分词器(内置+中文)、SpringBoot整合实战、仿京东商品搜索实战实现。
1.前言
条件语句
- match_all 查所有
- match 匹配查询
- bool联合查询
- term 词条精确查询
- range 范围查询
主要约束条件参数
- must 多条件时必须都满足-相当于MySQL条件中的 AND
- must_not 必须不匹配这些条件才可以
- should 相当于MySQL条件中的 OR
- filter 过滤条件-必须匹配
初始化测试数据
复杂查询之前,使用批量新增的方式,先初始化一些数据
- 索引(index):student_info
字段属性信息
- student_no(学号),name(名字),profession(专业),
- sex(性别1-男;2-女),age(年龄),socre(考试分),birthday(生日),desc(备注)
# 使用批量新增
POST _bulk
{
"index":{
"_index":"student_info","_id":11001}}
{
"id":11001,"student_no":"202011001","name":"张三","english_name":"zhangsan","profession":"计算机科学与技术","sex":1,"age":20,"score":601,"birthday":"2000-09-30","desc":"备注"}
{
"index":{
"_index":"student_info","_id":11002}}
{
"id":11002,"student_no":"202011002","name":"李四","english_name":"lisi","profession":"计算机科学与技术","sex":1,"age":21,"score":591,"birthday":"1999-06-15","desc":"我是李四的备注"}
{
"index":{
"_index":"student_info","_id":11003}}
{
"id":11003,"student_no":"202011003","name":"王五","english_name":"wangwu","profession":"计算机科学与技术","sex":1,"age":22,"score":599,"birthday":"1998-07-20","desc":"我是王五的备注"}
{
"index":{
"_index":"student_info","_id":11004}}
{
"id":11004,"student_no":"202011004","name":"赵六","english_name":"zhaoliu","profession":"软件工程","sex":1,"age":19,"score":620,"birthday":"2001-09-20","desc":"我是赵六的备注"}
{
"index":{
"_index":"student_info","_id":11005}}
{
"id":11005,"student_no":"202011005","name":"小七七","english_name":"xiaoqiqi","profession":"软件工程","sex":1,"age":20,"score":598,"birthday":"2000-09-11","desc":"小七七"}
{
"index":{
"_index":"student_info","_id":11006}}
{
"id":11006,"student_no":"202011006","name":"小小薇","english_name":"xiaoxiaowei","profession":"酒店管理","sex":2,"age":20,"score":621,"birthday":"2000-08-10","desc":"小小薇"}
{
"index":{
"_index":"student_info","_id":11007}}
{
"id":11007,"student_no":"202011007","name":"小芳芳","english_name":"xiaofangfang","profession":"酒店管理","sex":2,"age":19,"score":620,"birthday":"2001-09-10","desc":"小芳芳"}
{
"index":{
"_index":"student_info","_id":11008}}
{
"id":11008,"student_no":"202011008","name":"小小芳","english_name":"xiaoxiaofang","profession":"酒店管理","sex":2,"age":19,"score":620,"birthday":"2001-10-10","desc":"小小芳"}
2.简单查询
2.1查询所有结果(match_all)
GET /my_index1/_search
{
"query":{
"match_all":{
}}
}
2.2根据条件查询(query)
GET /my_index1/_search
{
"query":{
"match":{
"name":"张三"}},"sort":[{
"age":"desc"}]
}
2.3排序(sort)
添加 sort条件
GET /my_index1/_search
{
"query":{
"match":{
"name":"张三"}},"sort":[{
"age":"desc"}]
}
2.4指定查询返回结果字段(_source)
注:仅返回_source指定的字段
GET /my_index1/_search
{
"query":{
"match_all":{
}},"_source":["name","age"]
}
3.批量查询
3.1多ID查询(query)
GET /my_index3/_search
{
"query": {
"ids": {
"values":[ 1001, 1002, 1003 ]}}
}
3.2单索引批量查(mget)
# 批量查询my_index3索引数据
POST /my_index3/_mget
{
"ids": ["1001","1002","1003"]
}
3.3跨索引批量查(mget)
# 同时查询my_index1与my_index3两个所以下的数据
GET /_mget
{
"docs": [{
"_index": "my_index1","_id": "1001"},{
"_index": "my_index3","_id": "1002"},{
"index":"my_index1"}{
"query":{
"match_all":{
}},"from":0,"size":10}]
}
3.4跨索引批量查(msearch)
GET /_msearch
{
"index":"my_index1"}
{
"query":{
"match_all":{
}},"from":0,"size":10}
{
"index":"my_index3"}
{
"query":{
"match_all":{
}}}
3.匹配查询(match)
match原理是先分析文档,通过分词器进行解析,然后再去文档中查询。
3.1.关键字分词查询
先对match里面的字段值进行分词,然后进行查询
- “profession”: “计算机技术” :分词结果为 “计算机”、 “技术”,查询profession包含 "计算机"和 "技术"的记录
#关键字分词查询
GET /student_info/_search
{
"query": {
"match": {
"profession": "计算机技术"}}
}
#关键字分词查询-多个-逗号分隔
GET /student_info/_search
{
"query": {
"match": {
"profession": "计算机技术,酒店"}}
}
3.2.关联查询
“profession”: “计算机技术” :分词结果为 “计算机”、 “技术”,查询条件"operator": “and”,必须满足全部分词结果。
#关联查询 "operator": "and"
GET /student_info/_search
{
"query": {
"match": {
"profession": {
"query": "计算机技术","operator": "and"}}}
}
“profession”: “计算机技术” :分词结果为 “计算机”、 “技术”,查询条件"operator": “or”,任意满足其一。
#关联查询 "operator": "or"
GET /student_info/_search
{
"query": {
"match": {
"profession": {
"query": "计算机技术","operator": "or"}}}
}
3.3.多字段查询
检索内容"我计算机技术" , 会拆词为“我”,“计算机”,“技术”
匹配字段profession或desc字段中包含拆出来的词语的结果
#多字段查询
GET /student_info/_search
{
"query": {
"multi_match": {
"query": "我计算机技术","fields": [ "profession", "desc" ]}}
}
3.4.短语查询
match_phrase短语搜索,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致。
#短语查询
GET /student_info/_search
{
"query": {
"match_phrase": {
"profession": "计算机科"}}
}
3.5.高亮搜索(highlight)
- highlight 高亮查找
- pre_tags 标签前缀
- post_tags 标签后缀
- fields 规定的字段,支持多个
注意:如果不声明前缀和后缀,那边默认使用 <em></em>
GET /student_info/_search
{
"query":{
"match":{
"name":"张三"}},"highlight": {
"pre_tags":"<p class = \"text_high_light\">","post_tags":"</p>","fields": {
"name": {
}}}
}
3.6.前缀匹配
#前缀匹配查询
GET /student_info/_search
{
"query": {
"match_phrase_prefix": {
"name": "小小"}}
}
4.分页查询专题
详见:传送门
5.聚合查询专题
详见:传送门
6.模糊查询(fuzzy)
#模糊查询
GET /student_info/_search
{
"query": {
"fuzzy": {
"name": "张"}}
}
7.精确查询(trem)
term是关键词查询,参数类型type 一般都是是keyword , 精确查询,对查询的值不分词,直接进倒排索引去匹配。
- term 精确查找(单个)
- terms 精确查找(多个)
由于新增测试索引没有指定mapping >> “name”: “张三” 使用的是 text类型匹配
#关键词查询
GET /student_info/_search
{
"query": {
"term": {
"name.keyword": "张三"}}
}
terms表示多条件并列,用大括号 [ ] 涵盖所查内容,类似于MySql中in方法
# 多条件查询
GET /student_info/_search
{
"query": {
"terms": {
"age": [19,20,21,22]}}
}
8.范围查询
8.1range
大于-gt,小于-lt,大于等于-gte,小于等于-lte
数字范围
查询年龄 19 ~ 21
#范围查询
GET /student_info/_search
{
"query": {
"range": {
"age":{
"gte": 19,"lte": 21}}}
}
时间范围
#范围查询
GET /student_info/_search
{
"query": {
"range": {
"birthday":{
"gte": "2001-06-15","lte": "2001-09-20"}}}
}
8.2from … to
包含边界
#范围查询
GET /student_info/_search
{
"query": {
"range": {
"age":{
"from": 19,"to": 21}}}
}
范围查询不包含边界
#范围查询不包含边界
GET /student_info/_search
{
"query": {
"range": {
"age": {
"from": 19,"to": 21,"include_lower": false,"include_upper": false}}}
}
9.通配符查询(wildcard)
注:?用来匹配任意字符,*用来匹配零个或者多个字符,主要用于-英文检索
#通配符查询(*用来匹配零个或者多个字符)
GET /student_info/_search
{
"query": {
"wildcard": {
"english_name": "小*"}}
}
#通配符查询(?用来匹配任意字符)
GET /student_info/_search
{
"query": {
"wildcard": {
"english_name": "li?i"}}
}
10.must查询
must的多条件都必须满足
- must相当于MySQL条件中的 AND
GET /student_info/_search
{
"query": {
"bool": {
"must": [{
"match": {
"name": "小小"}},{
"range": {
"age": {
"gt": 19,"lte": 22}}}]}}
}
11.should查询
should的条件,至少满足一个就可以
- should相当于MySQL条件中的 OR
GET /student_info/_search
{
"query": {
"bool": {
"must": [{
"match": {
"name": "小小"}},{
"range": {
"age": {
"gt": 19,"lte": 22}}}]}}
}
12.filter过滤查询
#过滤查询
GET /student_info/_search
{
"query": {
"bool": {
"should": [{
"match": {
"name": "小小"}}],"filter": {
"range": {
"age": {
"gt": 21,"lte": 22}}}}}
}
附录
ES7.3版官方聚合查询API
关注程序员小强公众号更多编程趣事,知识心得与您分享
?关注“程序员小强”发送关键字“elasticSearch”到公众号获取相关篇