【问题标题】:Filter empty buckets from aggregation results从聚合结果中过滤空桶
【发布时间】:2016-10-02 06:42:54
【问题描述】:

因此,我们尝试过滤从 ElasticSearch 检索到的那些空存储桶,但没有成功。

通过聚合,我们可以找到:对于每种车辆颜色,对于每个时间戳,制造时间的总和。

在上一节中,我们编写了一个脚本,发现聚合超过了 1000 小时的制造时间。

它有效。不幸的是,我们收到的是empty buckets,而不是那些低于 1000 的过滤结果。

这是我们的查询:

{
 "size": 0,
 "aggs": {
  "colors": {
   "terms": {
    "field": "color"
    "min_doc_count": 1
   },
   "aggs": {
    "timestamps": {
     "terms": {
      "field": "timestamp",
      "min_doc_count": 1
     },
     "aggs": {
      "sum_manufacturing": {
       "sum": {
        "field": "hours"
       }
      },
      "manufacturing_bucket_filter": {
       "bucket_selector": {
        "buckets_path": {
         "hours": "sum_manufacturing"
        },
        "script": {
         "inline": "hours > 1000",
         "lang": "expression"
        }
       }
      }
     }
    }
   }
  }
 }
}

您会注意到我们在这里和那里添加了“min_doc_count” - 但它不会起到作用。

这是一个作为结果检索到的空桶:

{
  "key": "Yellow",
  "doc_count": 336,
  "timestamps": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 332,
    "buckets": [

    ]
  }
}

还有一个联合国空桶:

{
  "key": "Blue",
  "doc_count": 336,
  "timestamps": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 332,
    "buckets": [
      {
        "key": 1464880946000,
        "key_as_string": "2016-06-02T15:22:26.000Z",
        "doc_count": 4,
        "sum_manufacturing": {
          "value": 1049
        }
      }
    ]
  }
}

如果有办法过滤 out 空桶,那就太好了。 谢谢,任何帮助将不胜感激。

【问题讨论】:

    标签: elasticsearch aggregation buckets


    【解决方案1】:
    {
     "size": 0,
     "aggs": {
      "colors": {
       "terms": {
        "field": "color"
        "min_doc_count": 1
       },
       "aggs": {
        "timestamps": {
         "terms": {
          "field": "timestamp",
          "min_doc_count": 1
         },
         "aggs": {
          "sum_manufacturing": {
           "sum": {
            "field": "hours"
           }
          },
          "manufacturing_bucket_filter": {
           "bucket_selector": {
            "buckets_path": {
             "hours": "sum_manufacturing"
            },
            "script": {
             "inline": "hours > 1000",
             "lang": "expression"
            }
           }
          },
          "min_bucket_selector":{
              "bucket_selector": {
                "buckets_path": {"count": "sum_manufacturing._bucket_count"},
                "script": {"inline": "params.count != 0"}
              }
            }
         }
        }
       }
      }
     }
    }
    

    我认为这可能会解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2015-10-06
      • 2020-02-17
      • 2019-07-05
      • 2020-09-05
      • 1970-01-01
      • 2018-09-14
      相关资源
      最近更新 更多