根据salary工资分组聚合(显示出相同工资人数)
GET my_hello/_search
{
"size":0,
"aggs":{
"group_count":{
"terms":{"field":"salary"}
}
}
}
根据salary工资分组聚合(显示出相同工资人数),并且统计出每组的平均年龄
GET my_hello/_search
{
"size":0,
"aggs":{
"group_count":{
"terms":{"field": "salary"},
"aggs": {
"avg_age": {
"avg": {"field": "age"}
}
}
}
}
}
统计每个部门dep有多少人(这时的dep要用dep.keyword)
GET my_hello/_search
{
"size":0,
"aggs":{
"group_count":{
"terms": {"field": "dep"}
}
}
}
结果显示:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [dep] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
}。。。。。。。。。。。
这里提示我们要用keyword,正确代码:
GET my_hello/_search
{
"size":0,
"aggs":{
"group_count":{
"terms": {"field": "dep.keyword"}
}
}
}
Filter Aggregation Filter聚合用于过滤器聚合,把满足过滤器条件的文档分到一组。
计算男人的平均年龄 也就是统计gender字段包含关键字“男”的文档的age平均值。
统计男人人数并计算平均年龄
GET my_hello/_search
{
"size":0,
"aggs":{
"group_count":{
"filter": {
"term":{"gender":"男"}
},
"aggs":{
"avg_age":{
"avg": {"field": "age"
}
}
}
}
}
}
新建数据:
PUT /logs/message/_bulk?refresh
{ "index" : { "_id" : 1 } }
{ "body" : "warning: page could not be rendered" }
{ "index" : { "_id" : 2 } }
{ "body" : "authentication error" }
{ "index" : { "_id" : 3 } }
{ "body" : "warning: connection timed out" }
统计body中包含“error”和“warning”的个数