1 match和term查询
# 并且和或者的条件
#并且
GET t3/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "beautiful"
}
},
{
"match": {
"desc": "beautiful"
}
}
]
}
}
}
#或者
GET t3/doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "beautiful"
}
},
{
"match": {
"desc": "beautiful"
}
}
]
}
}
}
# match,term和terms的区别
-match查的短语会分词
GET w10/_doc/_search
{
"query": {
"match": {
"t1": "Beautiful girl!"
}
}
}
-term查的不会分词
GET w10/_doc/_search
{
"query": {
"term": {
"t1": "girl"
}
}
}
-terms由于部分词,想查多个,terms
GET w10/_doc/_search
{
"query": {
"terms": {
"t1": ["beautiful", "sexy"]
}
}
}
# pymysql 原生操作,查出字典
# orm orm直接转成对象
2 排序查询
##### 不是所有字段都支持排序,只有数字类型,字符串不支持
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
}
}
#降序
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
## 升序
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
GET lqz/_doc/_search
{
"query": {
"match_all": {
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
3 分页查询
#从第二条开始,取一条
GET lqz/_doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET lqz/_doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 2,
"size": 2
}
###注意:对于`elasticsearch`来说,所有的条件都是可插拔的,彼此之间用`,`分割
GET lqz/_doc/_search
{
"query": {
"match_all": {}
},
"from": 2,
"size": 2
}
4 布尔查询
- must(and)
- should(or)
- must_not(not)
##布尔查询之must and条件
GET lqz/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"from": "gu"
}
},
{
"match": {
"name": "顾老二"
}
}
]
}
}
}
##布尔查询之should or条件
GET lqz/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"from": "gu"
}
},
{
"match": {
"name": "龙套偏房"
}
}
]
}
}
}
### must_not条件 都不是
GET lqz/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"from": "gu"
}
},
{
"match": {
"tags": "可爱"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}
###filter,大于小于的条件 gt lt gte lte
GET lqz/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"from": "gu"
}
}
],
"filter": {
"range": {
"age": {
"lt": 30
}
}
}
}
}
}
### 范围查询
GET lqz/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"from": "gu"
}
}
],
"filter": {
"range": {
"age": {
"gte": 25,
"lte": 30
}
}
}
}
}
}
### filter需要在bool内部,并且如果是and条件,需要用must,如果使用了should,会认为是should和filter是或者的关系
5 查询结果过滤
###基本使用
GET lqz/_doc/_search
{
"query": {
"match_all": {
}
},
"_source":["name","age"]
}
####_source和query是平级的
GET lqz/_doc/_search
{
"query": {
"bool": {
"must":{
"match":{"from":"gu"}
},
"filter": {
"range": {
"age": {
"lte": 25
}
}
}
}
},
"_source":["name","age"]
}
6 高亮查询
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"highlight": {
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"from": {}
}
}
}
7 聚合函数
# sum ,avg, max ,min
# select max(age) as my_avg from 表 where from=gu;
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}
#最大年龄
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_max": {
"max": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}
#最小年龄
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_min": {
"min": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}
# 总年龄
#最小年龄
GET lqz/_doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}
#分组
# 现在我想要查询所有人的年龄段,并且按照`15~20,20~25,25~30`分组,并且算出每组的平均年龄。
GET lqz/_doc/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"age_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 15,
"to": 20
},
{
"from": 20,
"to": 25
},
{
"from": 25,
"to": 30
}
]
}
}
}
}