如何在术语聚合下的bucket_script聚合中使用总点击计数?

时间:2019-05-29 07:54:43

标签: elasticsearch elasticsearch-aggregation

我正在尝试将bucket_script聚合的总点击数用作术语聚合的子聚合。欢迎任何建议。

我还尝试在包ID字段上进行value_countterms聚合。但这也不起作用。

ES版本:6.7.1

映射:

PUT /packages/_mapping/_doc
{
  "properties": {
    "items": {
      "type": "nested"
    }
  }
}

示例文档:

{
  "id": 1,
  "name": "Lorem",
  "items": [
    {
      "infos": {
        "available": true
      },
      "item": {
        "id": 1,
        "meta": false,
        "name": "Ipsum"
      }
    },
    {
      "infos": {
        "available": false
      },
      "item": {
        "id": 2,
        "meta": false,
        "name": "Ipsum 2"
      }
    },
    {
      "infos": {
        "available": false
      },
      "item": {
        "id": 3,
        "meta": false,
        "name": "Ipsum 3"
      }
    }
  ]
}

我的聚合查询:

GET /packages/_search

{
  "query": { "match_all": {} },
  "size": 0,
  "aggs": {
    "items": {
      "nested": {
        "path": "items"
      },
      "aggs": {
        "non_meta": {
          "filter": {
            "term": {
              "items.item.meta": false
            }
          },
          "aggs": {
            "item": {
              "terms": {
                "field": "items.item.id"
              },
              "aggs": {
                "total_count": { <--------------------- **I want to get total document count here, but I can only reach `doc_count` for docs that includes items**
                  "reverse_nested": {}
                },
                "available_count": {
                  "sum": {
                    "script": {
                      "source": "doc['items.infos.available'].value == true ? 1 : 0"
                    }
                  }
                },
                "penetration": {
                  "bucket_script": {
                    "buckets_path": {
                      "available": "available_count",
                      "total": "total_count>_count"
                    },
                    "script": "params.available / params.total * 100"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

0 个答案:

没有答案