#记录聚合查询方法

from elasticsearch import Elasticsearch

es = Elasticsearch(['xx.xx.xx.xx:9200'])


#获取最小的年龄
res = es.search(index='test6', body = {
"query": {
"match_all": {}
},
"aggs": {
"min_age": {
"min": {
"field": "age"
}
}
}
})
print(res['aggregations']['min_age']['value'])


#获取最大的年龄
res = es.search(index='test6', body = {
"query": {
"match_all": {}
},
"aggs": {
"max_age": {
"max": {
"field": "age"
}
}
}
})
print(res['aggregations']['max_age']['value'])


#获取年龄和
res = es.search(index='test6', body = {
"query": {
"match_all": {}
},
"aggs": {
"sum_age": {
"sum": {
"field": "age"
}
}
}
})
print(res['aggregations']['sum_age']['value'])


#获取平均年龄
res = es.search(index='test6', body = {
"query": {
"match_all": {}
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
})
print(res['aggregations']['avg_age']['value'])

# from、size
#from:从“第几条”开始查询, size:查询多少条
res = es.search(index='test6', body = {
"query": {
"match_all": {}
},
"size": 1,
"from": 2
})
print(res)
posted on 2019-09-23 16:34  睡着的糖葫芦  阅读(3734)  评论(1编辑  收藏  举报