从聚合结果中过滤空桶

问题描述 投票:0回答:1

所以,我们试图过滤那些从 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 bucket
1个回答
0
投票
{
 "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"}
          }
        }
     }
    }
   }
  }
 }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.