代码语言:javascript复制
##查询所有查询
GET bank/_search
{
"query": {
"match_all": {}
},
"_source": ["account_number","balance"]
}
##全文检索,match的字段如果是一个文本,自动的进行分词,进行模糊匹配。
##倒排索引?
GET bank/_search
{
"query": {
"match": {
"address": "990 Mill Road"
}
}
}
##精确匹配,不进行分词模糊,keyword
GET bank/_search
{
"query": {
"match": {
"address.keyword": "990 Mill Road"
}
}
}
##精确匹配,不进行分词模糊,match_phrase
GET bank/_search
{
"query": {
"match_phrase": {
"address": "990 Mill Road"
}
}
}
##多字段匹配
GET bank/_search
{
"query": {
"multi_match": {
"query": "mill ak",
"fields": ["address","state"]
}
}
}
##复合查询;多个查询条件组合起来进行查询
##查询address里面包含Mill,并且gender是M,如果年龄在20-30之间最好,并且state必须不是IL
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "Mill"
}
},
{
"match_phrase": {
"gender": "M"
}
}
],
"must_not": [
{"match": {
"state": "IL"
}}
],
"should": [
{"range": {
"age": {
"gte": 20,
"lte": 30
}
}}
]
}
}
}
GET bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"gender": "M"
}}
]
}
}
}
##过滤查询;只过滤满足的结果,不评分。
##以后除了全文检索需要评分。剩下查询用过滤效果是一模一样,不用评分。query比filter,因为query要评分
GET bank/_search
{
"query": {
"bool": {
"filter": {
"match": {
"gender": "M"
}
}
}
}
}
##term查询;对于一些精确至的查询可以用term。
#term用于哪些类型不是文本的。age:22,status:true
#name:"";字符串不用来term。term某个字段必须匹配某个值;terms用来某个字段可以匹配多个值
##每一个是text类的字段,都有一个 属性.keyword字段,代表非text类型。可以用来精确匹配
GET bank/_search
{
"query": {
"terms": {
"age": [
"22",
"25"
]
}
}
}
GET bank/_search
{
"query": {
"term": {
"gender.keyword": "M"
}
}
}
##聚合:用来分析数据
GET bank/_search
##计算平均年龄
GET bank/_search
{
"aggs": {
"all_avg_age": {
"avg": {
"field": "age"
}
}
}
}
#搜索address中包含mill的所有人的年龄分布以及平均年龄。
GET bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"age_count": {
"terms": {
"field": "age",
"size": 1000
}
},
"age_avg":{
"avg": {
"field": "age"
}
}
}
}
##按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
GET bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"age_count": {
"terms": {
"field": "age"
},
"aggs": {
"avg_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
##查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"age_count": {
"terms": {
"field": "age"
},
"aggs": {
"gender_count":{
"terms": {
"field": "gender.keyword"
},
"aggs": {
"avg_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
##查出bank中所有高于平均薪资的这些男生以及他们的最高薪资和最低薪资,女生也一样。
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"avg_balance": {
"avg": {
"field": "balance"
}
},
"gender_count":{
"terms": {
"field": "gender.keyword"
},
"aggs": {
"gender_avg": {
"avg": {
"field": "balance"
}
}
}
}
}
}