如何在范围聚合中设置缺失字段的值?

时间:2016-12-07 14:00:02

标签: elasticsearch

我按this.addCharacter = {}; 进行分组,每个存储桶都有两个存储桶:offerIdprice <=0。我需要确保price > 0包含缺少price <= 0字段的文档:

price

我尝试在{ "size": 0, "aggs": { "by_offer_id": { "terms": { "field": "offerId" }, "aggs": { "by_price": { "range": { "field": "price", "ranges": [ { "to": 0 }, { "from": 0 } ] }, "aggs": { "price_stats": { "stats": { "field": "price" } } } } } } } } 之后添加"missing": 0,但会抛出"field": "price",

我使用的是SearchPhaseExecutionException,但可能会使用1.7.5的语法。

2 个答案:

答案 0 :(得分:0)

在这种特殊情况下,我不需要设置reduce

"missing" : 0

因为术语聚合返回{ "size": 0, "aggs": { "by_offer_id": { "terms": { "field": "offerId" }, "aggs": { "price_stats": { "stats": { "field": "price" } } } } } } 文档计数,白色total聚合仅包含具有现有价格的文档并返回总数。我可以通过减法推断出有多少文档没有价格字段。

答案 1 :(得分:0)

我认为您应该像这样使用脚本:

{
  "size": 0,
  "aggs": {
    "by_offer_id": {
      "terms": {
        "field": "offerId"
      },
      "aggs": {
        "by_price": {
          "range": {
            "script": {
              "lang": "painless",
              "source": "doc['price'].value ==null ? 0 : doc['price'].value"
            },
            "ranges": [
              {
                "to": 0
              },
              {
                "from": 0
              }
            ]
          },
          "aggs": {
            "price_stats": {
              "stats": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

"source": "doc['price'].value * 1"