es DSL 语句查询
es DSL 语句查询 实战1 : 连接
es DSL实战2 (日志查询): 连接
es DSL 查询特殊字符 (转义字符) :连接
ES DSL 精确检索term、terms 、range : 连接
全文检索match、multi_match、match_phrase、match_phrase_prefix : 连接
elasticsearch 提供了基于Json 的完整 query DSL(domain specific language ,领域特定语言) 来定义查询。
1测试数据准备:
三台集群, 数据在这:
es测试数据准备 : 连接
第二部分: DSL
所有测数据,都在postman上
0- 多字段匹配
GET ks-logstash-log-2024.02.22/_search
{
"query": {
"bool": {
"should": [
{ "match": { "kubernetes.container_name": "container-chat-info-prod" }},
{ "match": { "kubernetes.namespace_name": "cloud-prod" }},
{ "match": { "log": "error" }}
]
}
}
}
01.match匹配(模糊查询)
GET
GET http://10.0.0.111:19200/baimei/_search
{
"query":{
"match":{
"brand": "宝马"
}
}
}
2match_phrase (精确匹配)
GET http://10.0.0.111:19200/baimei/_search
{
"query":{
"match_phrase":{
"auther": "郭言金"
}
}
}
3match_all(全量查询)
GET http://10.0.0.111:19200/baimei/_search
{
"query":{
"match_all":{
}
}
}
04.分页查询
(size代表每页显示几条数据,from代表跳过数据的条数,查看第N页,from的值为:"(N-1)*size")
比如看第4页的, 每页有10条数据,那么 from 就是 3*10
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"match_phrase":{
"author": "梁金玉"
}
},
"size":3,
"from": 9
}
05.查看指定字段
GET http://10.0.0.111:19200/baimei/_search
{
"query":{
"match_phrase":{
"auther":"王彦昭"
}
},
"_source":["title","price","auther"]
}
06.排序
(sort代表排序,price代表基于价格进行排序,order表示排序的方式,分别为desc表示降序,asc表示升序。)
GET http://10.0.0.111:19200/baimei/_search
{
"query":{
"match_phrase":{
"auther": "张一博"
}
},
"sort":{
"price":{
"order": "desc"
}
}
}
升序:
{
"query":{
"match":{
"group": 3
}
},
"sort":{
"price":{
"order": "asc"
}
}
}
07-查询存在某个字段的文档
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"exists" : {
"field": "author"
}
}
}
08-语法高亮
(highlight表示进行语法高亮,pre_tags指定高亮的前缀标签,post_tags指定高亮的后缀标签,fields表示对哪个字段进行语法高亮。)
GET http://10.0.0.101:19200/baimei/_search
{
"query": {
"match": {
"title": "零食"
}
},
"sort": {
"price": {
"order": "desc"
}
},
"size":5,
"_source":["title","price","auther"]
,
"highlight": {
"pre_tags": [
"<span style='color:red;'>"
],
"post_tags": [
"</span>"
],
"fields": {
"title": {}
}
}
}
09-多条件查询
温馨提示:
bool:
可以匹配多个条件查询。其中有"must","must_not","should"。
"must"
必须匹配的条件。
"must_not"
必须不匹配的条件,即和must相反。
"should"
不是必要条件,满足其中之一即可,可以使用"minimum_should_match"来限制满足要求的条件数量
01-必要条件-must
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "Apple"
}
},
{
"match": {
"price": 5999.00
}
}
]
}
}
}
02-应该满足的条件-should
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"brand": "Apple"
}
},
{
"match": {
"brand": "保时捷"
}
},
{
"match": {
"price": 5999.00
}
}
],
"minimum_should_match": 2
}
}
}
03-必须不满足的条件-must_not
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"brand": "Apple"
}
},
{
"match": {
"price": 5999.00
}
}
]
}
}
}
10-范围查询
(filter代表过滤,range代表范围查询,price基于价格进行匹配,gte代表大于等于,lt代表小于。)
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "Apple"
}
}
],
"filter": {
"range": {
"price": {
"gte": 5499,
"lt": 9000
}
}
}
}
}
}
11-精确匹配多个值-terms
GET http://10.0.0.111:19200/baimei/_search
get baimei/_search
{
"query": {
"terms": {
"price": [
9899,
299,
4066
]
}
}
}
12-权重案例
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"price": 5999
}
}
],
"should": [
{
"match_phrase": {
"title": {
"query": "飞利浦",
"boost": 2
}
}
},
{
"match_phrase": {
"title": {
"query": "Apple",
"boost": 10
}
}
}
]
}
},
"highlight": {
"fields": {
"title": {},
"brand": {}
}
}
}
14-聚合查询
01.统计每个品牌的数量
GET http://10.0.0.111:19200/baimei/_search
{
"aggs": {
"baimeidashu_brand_group": {
"terms": {
"field": "brand"
}
}
},
"size": 0
}
02.统计5组商品最贵的
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"match": {
"group": 5
}
},
"aggs": {
"baimeidashu_max_shopping": {
"max": {
"field": "price"
}
}
},
"sort": {
"price": {
"order": "desc"
}
},
"_source": ["price","group","brand","title","auther"],
"size": 3
}
03.统计5组商品最便宜的
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"match": {
"group": 5
}
},
"aggs": {
"baimeidashu_min_shopping": {
"min": {
"field": "price"
}
}
},
"sort": {
"price": {
"order": "asc"
}
},
"_source": ["price","group","brand","title","auther"],
"size": 3
}
04.购买6组的平均商品价格
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"match": {
"group": 6
}
},
"aggs": {
"baimeidashu_avg_shopping": {
"avg": {
"field": "price"
}
}
},
"size": 0
}
05.购买7组所有商品需要多少钱
GET http://10.0.0.111:19200/baimei/_search
{
"query": {
"match": {
"group": 7
}
},
"aggs": {
"baimeidashu_sum_shopping": {
"sum": {
"field": "price"
}
}
},
"size": 0
}
欢迎来撩 : ELK集群汇总
欢迎来撩 : zabbix汇总
欢迎来撩 : ansible汇总
欢迎来撩 : shell 编程从0到1
欢迎来撩 : devops cicd持续集成全程(汇总)
欢迎来撩 : 汇总all
欢迎来撩 : 汇总all