python 进行es聚合查询
terms分组,定义聚合名称group_by_name
# terms分组,定义聚合名称group_by_name
query={
"aggs":{
"group_by_name":{
"terms":{
"field":"gender.keyword"
}
}
}
}
terms_value = es.search(index="account",body=query)
print(terms_value)
查询结果:
{
'took': 42,
'timed_out': False,
'_shards': {
'total': 1,
'successful': 1,
'skipped': 0,
'failed': 0
},
'hits': {
'total': {
'value': 5,
'relation': 'eq'
},
'max_score': 1.0,
'hits': [{
'_index': 'account',
'_type': 'test-type',
'_id': '7exk7YEBLWmmfdpNGSu5',
'_score': 1.0,
'_source': {
'id': 1,
'account_number': '000000',
'firstname': 'jcTang',
'lastname': 'Tang',
'age': 29,
'gender': '0',
'phone': 15100000000,
'address': '深圳市南山区xx路xx号xx房',
'email': 'jctang2022@euron.com',
'city': '深圳',
'state': '1'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '7uxn7YEBLWmmfdpNmSuf',
'_score': 1.0,
'_source': {
'id': 2,
'account_number': '000001',
'firstname': 'lilei',
'lastname': 'lei',
'age': 25,
'gender': '0',
'phone': 15900326519,
'address': '深圳市宝安区xx路xx号xx房 ',
'email ': 'lilei159 @gmail.com ',
'city ': '深圳 ',
'state ': '1 '
}
}, {
'_index ': 'account ',
'_type ': 'test - type ',
'_id ': '2 ',
'_score ': 1.0,
'_source ': {
'id ': 3,
'account_number ': '000002 ',
'firstname ': 'wangfang ',
'lastname ': 'wang ',
'age ': 27,
'gender ': '1 ',
'phone ': 13156968869,
'address ': '北京市昌平区xx路xx号xx房 ',
'email ': 'wangfang @163.com ',
'city ': '北京 ',
'state ': '0 '
}
}, {
'_index ': 'account ',
'_type ': 'test - type ',
'_id ': '3 ',
'_score ': 1.0,
'_source ': {
'id ': 4,
'account_number ': '000004 ',
'firstname ': 'weiwu ',
'lastname ': 'wei ',
'age ': 29,
'gender ': '1 ',
'phone ': 18926968869,
'address ': '上海市黄浦区xx路xx号xx房 ',
'email ': 'wei89548 @163.com ',
'city ': '上海 ',
'state ': '0 '
}
}, {
'_index ': 'account ',
'_type ': 'test - type ',
'_id ': '7 - yL7YEBLWmmfdpN7ytZ ',
'_score ': 1.0,
'_source ': {
'id ': 4,
'
'account_number ': '000004 ',
'firstname ': 'weiwu ',
'lastname ': 'wei ',
'age ': 29,
'gender ': '1 ',
'phone ': 18926968869,
'address ': '上海市黄浦区xx路xx号xx房 ',
'email ': 'wei89548 @163.com ',
'city ': '上海 ',
'state ': '0 '
}
}]
},
'aggregations ': {
'group_by_name ': {
'doc_count_error_upper_bound ': 0,
'sum_other_doc_count ': 0,
'buckets ': [{
'key ': '1 ',
'doc_count ': 3
}, {
'key ': '0 ',
'doc_count ': 2
}]
}
}
}
avg计算平均数
# avg计算平均数
query={
"aggs":{
"group_by_state":{
"terms":{
"field":"gender.keyword"
},
"aggs":{
"average_age":{
"avg":{
"field":"age"
}
}
}
}
}
}
avg_value = es.search(index="account",body=query)
print(avg_value)
查询结果:
{
'took': 28,
'timed_out': False,
'_shards': {
'total': 1,
'successful': 1,
'skipped': 0,
'failed': 0
},
'hits': {
'total': {
'value': 5,
'relation': 'eq'
},
'max_score': 1.0,
'hits': [{
'_index': 'account',
'_type': 'test-type',
'_id': '7exk7YEBLWmmfdpNGSu5',
'_score': 1.0,
'_source': {
'id': 1,
'account_number': '000000',
'firstname': 'jcTang',
'lastname': 'Tang',
'age': 29,
'gender': '0',
'phone': 15100000000,
'address': '深圳市南山区xx路xx号xx房',
'email': 'jctang2022@euron.com',
'city': '深圳',
'state': '1'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '7uxn7YEBLWmmfdpNmSuf',
'_score': 1.0,
'_source': {
'id': 2,
'account_number': '000001',
'firstname': 'lilei',
'lastname': 'lei',
'age': 25,
'gender': '0',
'phone': 15900326519,
'address': '深圳市宝安区xx路xx号xx房',
'email': 'lilei159@gmail.com',
'city': '深圳',
'state': '1'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '2',
'_score': 1.0,
'_source': {
'id': 3,
'account_number': '000002',
'firstname': 'wangfang',
'lastname': 'wang',
'age': 27,
'gender': '1',
'phone': 13156968869,
'address': '北京市昌平区xx路xx号xx房',
'email': 'wangfang@163.com',
'city': '北京',
'state': '0'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '3',
'_score': 1.0,
'_source': {
'id': 4,
'account_number': '000004',
'firstname': 'weiwu',
'lastname': 'wei',
'age': 29,
'gender': '1',
'phone': 18926968869,
'address': '上海市黄浦区xx路xx号xx房',
'email': 'wei89548@163.com',
'city': '上海',
'state': '0'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '7-yL7YEBLWmmfdpN7ytZ',
'_score': 1.0,
'_source': {
'id': 4,
'account_number': '000004',
'firstname': 'weiwu',
'lastname': 'wei',
'age': 29,
'gender': '1',
'phone': 18926968869,
'address': '上海市黄浦区xx路xx号xx房',
'email': 'wei89548@163.com',
'city': '上海',
'state': '0'
}
}]
},
'aggregations': {
'group_by_state': {
'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{
'key': '1',
'doc_count': 3,
'average_age': {
'value': 28.333333333333332
}
}, {
'key': '0',
'doc_count': 2,
'average_age': {
'value': 27.0
}
}]
}
}
}
order 排序
# order 排序
query={
"aggs":{
"group_by_name":{
"terms":{
"field":"gender.keyword",
"order":{"average_age":"desc"}
},
"aggs":{
"average_age":{
"avg":{
"field":"age"
}
}
}
}
}
}
order_value = es.search(index="account",body=query)
print(order_value)
查询结果:
{
'took': 66,
'timed_out': False,
'_shards': {
'total': 1,
'successful': 1,
'skipped': 0,
'failed': 0
},
'hits': {
'total': {
'value': 5,
'relation': 'eq'
},
'max_score': 1.0,
'hits': [{
'_index': 'account',
'_type': 'test-type',
'_id': '7exk7YEBLWmmfdpNGSu5',
'_score': 1.0,
'_source': {
'id': 1,
'account_number': '000000',
'firstname': 'jcTang',
'lastname': 'Tang',
'age': 29,
'gender': '0',
'phone': 15100000000,
'address': '深圳市南山区xx路xx号xx房',
'email': 'jctang2022@euron.com',
'city': '深圳',
'state': '1'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '7uxn7YEBLWmmfdpNmSuf',
'_score': 1.0,
'_source': {
'id': 2,
'account_number': '000001',
'firstname': 'lilei',
'lastname': 'lei',
'age': 25,
'gender': '0',
'phone': 15900326519,
'address': '深圳市宝安区xx路xx号xx房',
'email': 'lilei159@gmail.com',
'city': '深圳',
'state': '1'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '2',
'_score': 1.0,
'_source': {
'id': 3,
'account_number': '000002',
'firstname': 'wangfang',
'lastname': 'wang',
'age': 27,
'gender': '1',
'phone': 13156968869,
'address': '北京市昌平区xx路xx号xx房',
'email': 'wangfang@163.com',
'city': '北京',
'state': '0'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '3',
'_score': 1.0,
'_source': {
'id': 4,
'account_number': '000004',
'firstname': 'weiwu',
'lastname': 'wei',
'age': 29,
'gender': '1',
'phone': 18926968869,
'address': '上海市黄浦区xx路xx号xx房',
'email': 'wei89548@163.com',
'city': '上海',
'state': '0'
}
}, {
'_index': 'account',
'_type': 'test-type',
'_id': '7-yL7YEBLWmmfdpN7ytZ',
'_score': 1.0,
'_source': {
'id': 4,
'account_number': '000004',
'firstname': 'weiwu',
'lastname': 'wei',
'age': 29,
'gender': '1',
'phone': 18926968869,
'address': '上海市黄浦区xx路xx号xx房',
'email': 'wei89548@163.com',
'city': '上海',
'state': '0'
}
}]
},
'aggregations': {
'group_by_name': {
'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{
'key': '1',
'doc_count': 3,
'average_age': {
'value': 28.333333333333332
}
}, {
'key': '0',
'doc_count': 2,
'average_age': {
'value': 27.0
}
}]
}
}
}
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/273704.html