繁体   English   中英

查找nested.nested Elastic Search 文档的数量

[英]Find the count of nested.nested Elastic Search documents

是否有可能使用弹性搜索_count API并具有以下简称ES模板找到的计数sponsorships全部由活动brandId

sponsorshipSetssponsorships是可选的,所以它可以是null

{
  "index_patterns": "campaigns*",
  "order": 4,
  "version": 4,
  "aliases": {
    "campaigns": {

    }
  },
  "settings": {
    "number_of_shards": 5
  },
  "mappings": {
    "dynamic": "false",
    "properties": {
      "brandId": {
        "type": "keyword"
      },
      "sponsorshipSets": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "keyword"
          },
          "sponsorships": {
            "type": "nested",
            "properties": {
              "id": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }

过滤器聚合可用于获取具有特定品牌 ID 的文档。 两个嵌套聚合指向赞助和 value_count 聚合以获取计数。

询问

{
  "aggs": {
    "selected_brand": {
      "filter": {
        "term": {
          "brandId": "1"
        }
      }
    },
    "sponsorshipSets": {
      "nested": {
        "path": "sponsorshipSets"
      },
      "aggs": {
        "sponsorships": {
          "nested": {
            "path": "sponsorshipSets.sponsorships"
          },
          "aggs": {
            "count": {
              "value_count": {
                "field": "sponsorshipSets.sponsorships.id"
              }
            }
          }
        }
      }
    }
  }
}

我找到了一个不使用聚合的解决方案,从上面看起来更准确,我可以使用_count API。

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "sponsorshipSets.sponsorships",
                        "query": {
                            "bool": {
                                "filter": {
                                    "exists": {
                                        "field": "sponsorshipSets.sponsorships"
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "term": {
                        "brandId": "b1d28821-3730-4266-8f55-eb69596004fb"
                    }
                }
            ]
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM