首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找nested.nested弹性搜索文档的计数

查找nested.nested弹性搜索文档的计数
EN

Stack Overflow用户
提问于 2020-03-10 01:11:57
回答 2查看 30关注 0票数 0

是否可以使用弹性搜索_count API并使用以下简短的ES模板来为brandId的所有活动查找sponsorships计数

sponsorshipSetssponsorships是可选的,因此可以是null

代码语言:javascript
复制
{
  "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"
              }
            }
          }
        }
      }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-10 21:16:10

我找到了一种不用聚集的解决方案,从上面看,它似乎更准确,而且我可以使用_count API。

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "sponsorshipSets.sponsorships",
                        "query": {
                            "bool": {
                                "filter": {
                                    "exists": {
                                        "field": "sponsorshipSets.sponsorships"
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "term": {
                        "brandId": "b1d28821-3730-4266-8f55-eb69596004fb"
                    }
                }
            ]
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-03-10 02:06:18

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

查询

代码语言:javascript
复制
{
  "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"
              }
            }
          }
        }
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60610328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档