Elasticsearch聚合之bucket桶

存储桶聚合不会像指标聚合那样计算字段上的指标,而是创建存储桶。 每个桶与标准相关联(取决于聚合类型),该标准确定当前上下文中的文档是否“落入”其中。 换句话说,存储桶有效地定义了文档集。 除了桶本身之外,桶聚合还计算并返回“落入”每个桶的文档数

adjacency_matrix

adjacency_matrix类型的聚合可以返回类似于集合的操作,例如A,B,C三个筛选条件,聚合请求就会返回符合A的,符合A&B的,符合B的,符合B&C的,符合C的,符合A&C的不同集合的数据数目。
例如:

GET emails/message/_search
{
  "size": 0,
  "aggs" : {
    "interactions" : {
      "adjacency_matrix" : {
        "filters" : {
          "grpA" : { "terms" : { "accounts" : ["hillary", "sidney"] }},
          "grpB" : { "terms" : { "accounts" : ["donald", "mitt"] }},
          "grpC" : { "terms" : { "accounts" : ["vladimir", "nigel"] }}
        }
      }
    }
  }
}

返回, 集合条数为0的筛选条件则不返回,例如grepA&grepC:

{
  "took": 9,
  "timed_out": false,
  "_shards": ...,
  "hits": ...,
  "aggregations": {
    "interactions": {
      "buckets": [
        {
          "key":"grpA",
          "doc_count": 2
        },
        {
          "key":"grpA&grpB",
          "doc_count": 1
        },
        {
          "key":"grpB",
          "doc_count": 2
        },
        {
          "key":"grpB&grpC",
          "doc_count": 1
        },
        {
          "key":"grpC",
          "doc_count": 1
        }
      ]
    }
  }
}

子聚合

子聚合可以聚合是父子关系的数据集。
请求示例:

POST child_example/_search?size=0
{
  "aggs": {
    "top-tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 10
      },
      "aggs": {
        "to-answers": {
          "children": {
            "type" : "answer" 
          },
          "aggs": {
            "top-names": {
              "terms": {
                "field": "owner.display_name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

按时间维度聚合

date_histogram可以按时间维度聚合,结果返回按一定时间间隔分隔的数据条数,请求示例如下,其中interval字段可以是year,quarter,month,week,day,hour,minute,second。也可以是自定义的时间,例如1M(1个月),1m(1分钟)等等。format字段决定了返回结果中的key值的格式化样式。

POST /sales/_search?size=0
{
    "aggs" : {
        "sales_over_time" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "1M",
                "format" : "yyyy-MM-dd",
                "time_zone": "-01:00"
            }
        }
    }
}

返回结果:

{
    ...
    "aggregations": {
        "sales_over_time": {
            "buckets": [
                {
                    "key_as_string": "2015-01-01",
                    "key": 1420070400000,
                    "doc_count": 3
                },
                {
                    "key_as_string": "2015-02-01",
                    "key": 1422748800000,
                    "doc_count": 2
                },
                {
                    "key_as_string": "2015-03-01",
                    "key": 1425168000000,
                    "doc_count": 2
                }
            ]
        }
    }
}

可以添加参数"keyed": true,这样返回结果就会变成这样:

 "2015-01-01": {
     "key_as_string": "2015-01-01",
     "key": 1420070400000,
     "doc_count": 3
 },
posted @   松松哥、  阅读(59)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示