代码之家  ›  专栏  ›  技术社区  ›  Dinosaurius

如何计算“value”大于0的桶总数?

  •  0
  • Dinosaurius  · 技术社区  · 7 年前

    我有一个查询,它计算每个bucket的事件数。如何计算具有 value 大于 0 ?

    GET myindex/_search?
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "PlateNumber": "111"
              }
            }
          ]
        }
      },
      "aggs": {
        "daily_intensity": {
          "date_histogram": {
            "field": "Datetime",
            "interval": "day"
          },
          "aggs": {
            "count_of_events": {
              "value_count": {
                "field": "Monthday"
              }
            }
          }
        }
      }
    }
    

    这是我得到的输出。我想要得到的预期答案是 26 ,因为桶中总共有26个元素 价值

    {
      "took": 237,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 98,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "daily_intensity": {
          "buckets": [
            {
              "key_as_string": "2017-05-01T00:00:00.000Z",
              "key": 1493596800000,
              "doc_count": 3,
              "count_of_events": {
                "value": 3
              }
            },
            {
              "key_as_string": "2017-05-02T00:00:00.000Z",
              "key": 1493683200000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-03T00:00:00.000Z",
              "key": 1493769600000,
              "doc_count": 4,
              "count_of_events": {
                "value": 4
              }
            },
            {
              "key_as_string": "2017-05-04T00:00:00.000Z",
              "key": 1493856000000,
              "doc_count": 6,
              "count_of_events": {
                "value": 6
              }
            },
            {
              "key_as_string": "2017-05-05T00:00:00.000Z",
              "key": 1493942400000,
              "doc_count": 0,
              "count_of_events": {
                "value": 0
              }
            },
            {
              "key_as_string": "2017-05-06T00:00:00.000Z",
              "key": 1494028800000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-07T00:00:00.000Z",
              "key": 1494115200000,
              "doc_count": 5,
              "count_of_events": {
                "value": 5
              }
            },
            {
              "key_as_string": "2017-05-08T00:00:00.000Z",
              "key": 1494201600000,
              "doc_count": 6,
              "count_of_events": {
                "value": 6
              }
            },
            {
              "key_as_string": "2017-05-09T00:00:00.000Z",
              "key": 1494288000000,
              "doc_count": 2,
              "count_of_events": {
                "value": 2
              }
            },
            {
              "key_as_string": "2017-05-10T00:00:00.000Z",
              "key": 1494374400000,
              "doc_count": 3,
              "count_of_events": {
                "value": 3
              }
            },
            {
              "key_as_string": "2017-05-11T00:00:00.000Z",
              "key": 1494460800000,
              "doc_count": 0,
              "count_of_events": {
                "value": 0
              }
            },
            {
              "key_as_string": "2017-05-12T00:00:00.000Z",
              "key": 1494547200000,
              "doc_count": 3,
              "count_of_events": {
                "value": 3
              }
            },
            {
              "key_as_string": "2017-05-13T00:00:00.000Z",
              "key": 1494633600000,
              "doc_count": 0,
              "count_of_events": {
                "value": 0
              }
            },
            {
              "key_as_string": "2017-05-14T00:00:00.000Z",
              "key": 1494720000000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-15T00:00:00.000Z",
              "key": 1494806400000,
              "doc_count": 3,
              "count_of_events": {
                "value": 3
              }
            },
            {
              "key_as_string": "2017-05-16T00:00:00.000Z",
              "key": 1494892800000,
              "doc_count": 0,
              "count_of_events": {
                "value": 0
              }
            },
            {
              "key_as_string": "2017-05-17T00:00:00.000Z",
              "key": 1494979200000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-18T00:00:00.000Z",
              "key": 1495065600000,
              "doc_count": 3,
              "count_of_events": {
                "value": 3
              }
            },
            {
              "key_as_string": "2017-05-19T00:00:00.000Z",
              "key": 1495152000000,
              "doc_count": 2,
              "count_of_events": {
                "value": 2
              }
            },
            {
              "key_as_string": "2017-05-20T00:00:00.000Z",
              "key": 1495238400000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-21T00:00:00.000Z",
              "key": 1495324800000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-22T00:00:00.000Z",
              "key": 1495411200000,
              "doc_count": 5,
              "count_of_events": {
                "value": 5
              }
            },
            {
              "key_as_string": "2017-05-23T00:00:00.000Z",
              "key": 1495497600000,
              "doc_count": 16,
              "count_of_events": {
                "value": 16
              }
            },
            {
              "key_as_string": "2017-05-24T00:00:00.000Z",
              "key": 1495584000000,
              "doc_count": 4,
              "count_of_events": {
                "value": 4
              }
            },
            {
              "key_as_string": "2017-05-25T00:00:00.000Z",
              "key": 1495670400000,
              "doc_count": 6,
              "count_of_events": {
                "value": 6
              }
            },
            {
              "key_as_string": "2017-05-26T00:00:00.000Z",
              "key": 1495756800000,
              "doc_count": 1,
              "count_of_events": {
                "value": 1
              }
            },
            {
              "key_as_string": "2017-05-27T00:00:00.000Z",
              "key": 1495843200000,
              "doc_count": 5,
              "count_of_events": {
                "value": 5
              }
            },
            {
              "key_as_string": "2017-05-28T00:00:00.000Z",
              "key": 1495929600000,
              "doc_count": 4,
              "count_of_events": {
                "value": 4
              }
            },
            {
              "key_as_string": "2017-05-29T00:00:00.000Z",
              "key": 1496016000000,
              "doc_count": 5,
              "count_of_events": {
                "value": 5
              }
            },
            {
              "key_as_string": "2017-05-30T00:00:00.000Z",
              "key": 1496102400000,
              "doc_count": 2,
              "count_of_events": {
                "value": 2
              }
            },
            {
              "key_as_string": "2017-05-31T00:00:00.000Z",
              "key": 1496188800000,
              "doc_count": 4,
              "count_of_events": {
                "value": 4
              }
            }
          ]
        }
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Hatim Stovewala    7 年前

    您可以使用 Bucket Script Aggregation & Sum Bucket Aggregation 为了实现这一点。尝试下面的查询。

    GET myindex/_search?
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "PlateNumber": "111"
              }
            }
          ]
        }
      },
      "aggs": {
        "daily_intensity": {
          "date_histogram": {
            "field": "Datetime",
            "interval": "day"
          },
          "aggs": {
            "count_of_events": {
              "value_count": {
                "field": "Monthday"
              }
            },
            "check": {
              "bucket_script": {
                "buckets_path": {
                  "count": "count_of_events"
                },
                "script": "return (params.count > 0 ? 1 : 0)"
              }
            }
          }
        },
        "bucket_count": {
          "sum_bucket": {
            "buckets_path": "daily_intensity>check"
          }
        }
      }
    }