根据子聚合结果文档数选择聚合

时间:2019-10-03 05:51:20

标签: elasticsearch

我的目标是仅选择在子聚合中定义了min_doc_count个匹配项的那些聚合。不知道是否可能。 基本上,我只想选择那些具有属于特定导入的propertyid的存储桶。 这是我的查询。

GET properties/_search
{
  "size": 0,
  "query": {
    "terms": {
      "Agency_Id": [
        "16"
      ]
    }
  },
  "aggregations": {
    "property_id": {
      "terms": {
        "field": "PropertyId",
        "min_doc_count": 2,
        "size": 10000
      },
      "aggregations": {
        "import_filter": {
          "filter": {
            "term": {
              "Import_Id": "90040"
            }
          },
          "aggregations": {
            "import_id": {
              "terms": {
                "field": "Import_Id",
                "min_doc_count": 1,
                "size": 10000
              }
            }
          }
        }
      }
    }
  }
}

实际结果

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1163,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "property_id" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "011162330",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : "6065590",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : "6289352",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : "gd-00-022386",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 0,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [ ]
            }
          }
        }
      ]
    }
  }
}

预期

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1163,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "property_id" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "011162330",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : "6065590",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        },
        {
          "key" : "6289352",
          "doc_count" : 2,
          "import_filter" : {
            "doc_count" : 1,
            "import_id" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : 90040,
                  "doc_count" : 1
                }
              ]
            }
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:1)

根据我对您查询的理解,您需要Bucket selector aggregation 查询:

GET properties/_search
{
  "size": 0,
  "query": {
    "terms": {
      "Agency_Id": [
        "16"
      ]
    }
  },
  "aggregations": {
    "property_id": {
      "terms": {
        "field": "PropertyId",
        "min_doc_count": 2,
        "size": 10000
      },
      "aggregations": {
        "import_filter": {
          "filter": {
            "term": {
              "Import_Id": "90040"
            }
          },
          "aggregations": {
            "import_id": {
              "terms": {
                "field": "Import_Id",
                "min_doc_count": 1,
                "size": 10000
              }
            }
          }
        },
        "mybucket_selector": { ---> select product bucket if import bucket has any value
          "bucket_selector": {
            "buckets_path": {
              "FinalCount": "import_filter>import_id._bucket_count"
            },
            "script": "params.FinalCount>0"
          }
        }
      }
    }
  }
}