嵌套聚合和查询不起作用

holgip5t  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(189)

请找人帮我回答下面的问题。

https://discuss.elastic.co/t/nested-aggregation-with-and-always-return-0-match/315722?u=chattes

rbl8hiat

rbl8hiat1#

我使用了以下聚合

1. Terms aggregation

2. Bucket selector

3. Nested aggregation

首先,我使用术语聚合按用户ID分组。然后按技能ID进一步分组。使用存储桶选择器,我筛选了拥有两种技能下的文档的用户。

查询

GET index5/_search
{
  "size": 0,
  "aggs": {
    "users": {
      "terms": {
        "field": "id",
        "size": 10
      },
      "aggs": {
        "skills": {
          "nested": {
            "path": "skills"
          },
          "aggs": {
            "filter_skill": {
              "terms": {
                "field": "skills.id",
                "size": 10,
                "include": [
                  553,
                  426
                ]
              }
            }
          }
        },
        "bucket_count": {
          "bucket_selector": {
            "buckets_path": {
              "skill_count": "skills>filter_skill._bucket_count"
            },
            "script": "params.skill_count ==2"
          }
        }
      }
    }
  }
}

结果

"aggregations" : {
    "users" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1,
          "skills" : {
            "doc_count" : 3,
            "filter_skill" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "426",
                  "doc_count" : 1
                },
                {
                  "key" : "553",
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : 2,
          "doc_count" : 1,
          "skills" : {
            "doc_count" : 2,
            "filter_skill" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "426",
                  "doc_count" : 1
                },
                {
                  "key" : "553",
                  "doc_count" : 1
                }
              ]
            }
          }
        }
      ]
    }

相关问题