es – elasticsearch – aggs – metrics – top_metrics

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问:top_metrics有什么特点?
答:在这里插入图片描述
问:top_metrics如何使用?
答:

DELETE /top_metrics_test

PUT /top_metrics_test
{
  "mappings": {
    "properties": {
      "type": {"type": "integer"},
      "num": {"type": "integer"},
      "date": {"type": "date"}
    }
  }
}

POST /top_metrics_test/_doc/1
{
  "type": 1,
  "num": 3,
  "date": "2021-09-08"
}


POST /top_metrics_test/_doc/2
{
  "type": 2,
  "num": 7,
  "date": "2021-09-08"
}


POST /top_metrics_test/_doc/3
{
  "name": "me",
  "type": 1,
  "num": 5,
  "date": "2021-09-08"
}

POST /top_metrics_test/_doc/4
{
  "type": 1,
  "num": 1,
  "date": "2019-07-18"
}

POST /top_metrics_test/_doc/5
{
  "type": 2,
  "num": 9,
  "date": "2019-07-18"
}

GET /top_metrics_test/_search
{
  "size": 0,
  "aggs": {
    "type_top_aggs": {
      "terms": {
        "field": "type",
        "size": 10
      },
      "aggs": {
        "top_aggs": {
          "top_metrics": {
            "metrics": [
              {"field": "type"},
              {"field": "date"}
            ],
            "size": 2,
            "sort": {"num": "desc"}
          }
        }
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "type_top_aggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 3,
          "top_aggs" : {
            "top" : [
              {
                "sort" : [
                  5
                ],
                "metrics" : {
                  "type" : 1,
                  "date" : "2021-09-08T00:00:00.000Z"
                }
              },
              {
                "sort" : [
                  3
                ],
                "metrics" : {
                  "type" : 1,
                  "date" : "2021-09-08T00:00:00.000Z"
                }
              }
            ]
          }
        },
        {
          "key" : 2,
          "doc_count" : 2,
          "top_aggs" : {
            "top" : [
              {
                "sort" : [
                  9
                ],
                "metrics" : {
                  "type" : 2,
                  "date" : "2019-07-18T00:00:00.000Z"
                }
              },
              {
                "sort" : [
                  7
                ],
                "metrics" : {
                  "type" : 2,
                  "date" : "2021-09-08T00:00:00.000Z"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

GET /top_metrics_test/_search
{
  "size": 0,
  "aggs": {
    "type_top_aggs": {
      "terms": {
        "field": "date",
        "size": 10
      },
      "aggs": {
        "top_aggs": {
          "top_metrics": {
            "metrics": [
              {"field": "type"},
              {"field": "num"}
            ],
            "size": 1,
            "sort": {"num": "desc"}
          }
        }
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "type_top_aggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1631059200000,
          "key_as_string" : "2021-09-08T00:00:00.000Z",
          "doc_count" : 3,
          "top_aggs" : {
            "top" : [
              {
                "sort" : [
                  7
                ],
                "metrics" : {
                  "type" : 2,
                  "num" : 7
                }
              }
            ]
          }
        },
        {
          "key" : 1563408000000,
          "key_as_string" : "2019-07-18T00:00:00.000Z",
          "doc_count" : 2,
          "top_aggs" : {
            "top" : [
              {
                "sort" : [
                  9
                ],
                "metrics" : {
                  "type" : 2,
                  "num" : 9
                }
              }
            ]
          }
        }
      ]
    }
  }
}

THE END
分享
二维码
< <上一篇

)">
下一篇>>