4

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

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

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

有用。不幸的是,我们收到了一个空桶,而不是那些低于 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
        }
      }
    ]
  }
}

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

4

1 回答 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"}
          }
        }
     }
    }
   }
  }
 }
}

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

于 2018-03-26T12:41:05.570 回答