如果doc_count == 0,然后弹性搜索,否则为1,否则为0

时间:2018-08-22 05:48:24

标签: json for-loop if-statement elasticsearch clause

已检查的存储桶脚本聚合 父管道聚合执行脚本,该脚本可以在父多桶聚合中的指定指标上按桶进行计算。指定的指标必须为数字,并且脚本必须返回数字值。

使用存储区脚本时 我有以下查询,但是当_count == 0或[]时,无法为product_id = 6435532打印1000,而是不打印任何内容。请帮忙。 _count> 0时,它工作正常。

POST index_name / _search ? size = 0 {
  "aggs": {
    "group by products": {
      "terms": {
        "field": "product_id.keyword",

      },
      "aggs": {
        "filter for PVW": {
          "filter": {
            "term": {
              "event_type.keyword": "PVW"
            }
          }
        },
        "group_by_CIP": {
          "terms": {
            "field": "event_type.keyword",
            "include": "CIP"

          },
          "aggregations": {
            "print product id": {
              "aggs": {
                "CIP doc count": {
                  "bucket_script": {
                    "buckets_path": {
                      "var1": "_count"

                    },
                    "script": "if (params.var1 == 'N/A') {1000 } else { params.var1 }"--unable to print 1000
                    for _count value Null or 0 value
                  }
                }
              },
              "terms": {
                "field": "web_prod_id.keyword"
              }
            }
          }
        }
      }
    }
  }
}

输出看起来像

"aggregations": {
  "group by products": {
    "doc_count_error_upper_bound": 111,
    "sum_other_doc_count": 115547,
    "buckets": [{
        "key": "6435532",
        "doc_count": 282,
        "group_by_CIP": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": []
        },
        "filter for PVW": {
          "doc_count": 282
        }
      },
      {
        "key": "5697273",
        "doc_count": 247,
        "group_by_CIP": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [{
            "key": "CIP",
            "doc_count": 13,
            "print product id": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [{
                "key": "5697273",
                "doc_count": 13,
                "CIP doc count": {
                  "value": 13
                }
              }]
            }
          }]
        },
        "filter for PVW": {
          "doc_count": 234
        }
      }
    ]
  }
}
}

有人遇到同样的问题吗?

0 个答案:

没有答案