条件查询
query:查询
match:匹配
match_all:匹配所有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #第一种 GET /shopping/_search?q=名字:张三
#第二种 GET /shopping/_search { "query": { "match": { "名字": "张三" } } }
#全量查询 match_all GET /shopping/_search { "query": { "match_all": { } } }
|
分页查询
from开始计算公式:(页码-1) * 每页数据条数
from:表示从第几行开始
size:表示查询多少条文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #查询从0行开始 GET /shopping/_search { "query": { "match_all": { } }, "from": 0, "size": 2 }
#数据源过滤,只查找_source包含名字的行 GET /shopping/_search { "query": { "match_all": { } }, "from": 0, "size": 2, "_source": ["名字"] }
|
查询排序
order:排序
desc:降序
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 降序排序,按照年龄降序搜索名字 GET /shopping/_search { "query": { "match_all": { } }, "_source": ["名字"], "sort": { "年龄":{ "order" : "desc" } } }
|
多条件查询
bool:条件
must:类似and,必须 多条件同时成立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #条件同时成立,名字为张三和年龄为36岁 GET /shopping/_search { "query": { "bool":{ "must": [ { "match": { "名字": "张三" } }, { "match": { "年龄": 36 } } ] } } }
|
should:查询类似or,或者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #条件为搜索名字为张三或李四 GET /shopping/_search { "query": { "bool":{ "should": [ { "match": { "名字": "张三" } }, { "match": { "名字": "李四" } } ] } } }
|
范围查询
filter:过滤
range:范围
gte:大于
lte:小于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #条件查询名字张三或李四年龄大于35岁到40岁之间 GET /shopping/_search { "query": { "bool":{ "should": [ { "match": { "名字": "张三" } }, { "match": { "名字": "李四" } } ], "filter": [ { "range": { "年龄": { "gte": 35, "lte": 40 } } } ] } } }
|
全文检索
在es中,有文字的一部分也能正常查询到数据,es会将内容分词在倒排索引中匹配,比如“张三”,匹配“张”或者“三”都会进行匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| GET /shopping/_search { "query": { "match": { "名字": "张" } } }
GET /shopping/_search { "query": { "match": { "名字": "三" } } }
|

完全匹配
match_phrase:完全匹配
1 2 3 4 5 6 7 8
| GET /shopping/_search { "query": { "match_phrase": { "名字": "张三" } } }
|
高亮查询
highlight:高亮字段
其实就是特殊的内容进行样式的设定
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #对名字高亮显示 GET /shopping/_search { "query": { "match_phrase": { "名字": "张三" } }, "highlight": { "fields": { "名字": {} } } }
|

聚合查询
aggs:聚合操作
1 2 3 4 5 6 7 8 9 10 11
| #将所有年龄分组分别统计出来 GET /shopping/_search { "aggs":{ "age_group": { "terms": { "field": "年龄" } } } }
|

1 2 3 4 5 6 7 8 9 10 11
| GET /shopping/_search { "aggs":{ "age_group": { "terms": { "field": "年龄" } } }, "size": 0 }
|
1 2 3 4 5 6 7 8 9 10 11 12
| #统计结果为年龄的平均值 GET /shopping/_search { "aggs":{ "age_agv": { "avg": { "field": "年龄" } } }, "size": 0 }
|
映射关系
properties:特性
sex:性别
keyword:关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #创建索引,并定义映射 PUT /user PUT /user/_mapping { "properties" : { "name" : { "type" : "text", "index" : true }, "sex": { "type" : "keyword", "index" : true }, "phone": { "type": "keyword", "index" : false } } }
|
1 2 3 4 5 6 7
| #user索引创建数据 PUT /user/_create/1001 { "name": "小米", "sex": "man", "phone": 123456789 }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #查询name模糊匹配值存在,因为创建时type为text GET /user/_search { "query": { "match": { "name": "小" } } }
#查询sex模糊匹配值为空,因为创建时type为keyword GET /user/_search { "query": { "match": { "sex": "ma" } } }
#查询phone匹配为空,因为创建时index为false,不能被索引查询 GET /user/_search { "query": { "match": { "phone": "123456" } } }
|