【问题标题】:Frequency count aggregation of nested elements in elastic search returning zero results弹性搜索中嵌套元素的频率计数聚合返回零结果
【发布时间】:2020-07-26 02:50:51
【问题描述】:

我正在尝试进行聚合查询以计算嵌套在弹性搜索中的元素数量。这是映射的一部分:

 {
  "news" : {
    "aliases" : { },
    "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "categories" : {
          "type" : "nested",
          "properties" : {
            "categoryId" : {
              "type" : "text"
            },
            "name" : {
              "type" : "text"
            },
            "slug" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

这是我做的查询:

{   
    "size": 0,
    "aggs":{
        "categories_count":{
            "terms": {
                "field":"categories.slug.keyword",
                "size": 100
            }
        }
    }
}

这是我得到的结果:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 22,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "categories_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": []
        }
    }
}

这是我想要实现的目标: 说有两个文件:

doc 1 类别:

"categories" : [
      {
        "categoryId" : "aaaaffff",
        "name" : "Category 1",
        "slug" : "category-1",
      },
      {
        "categoryId" : "aaaafffd",
        "name" : "Category 2",
        "slug" : "category-2",
      },
      {
        "categoryId" : "aaaafffc",
        "name" : "Category 3",
        "slug" : "category-3",
      }
    ],

文档 2 类别:

"categories" : [
          {
            "categoryId" : "aaaafffd",
            "name" : "Category 2",
            "slug" : "category-2",
          },
          {
            "categoryId" : "aaaafffc",
            "name" : "Category 3",
            "slug" : "category-3",
          },
          {
            "categoryId" : "aaaafffb",
            "name" : "Category 4",
            "slug" : "category-4",
          },
          {
            "categoryId" : "aaaafffe",
            "name" : "Category 5",
            "slug" : "category-5",
          },
        ],

输出应该是这样的:

{
    {
        "categoryId": "aaaaffff",
        "name": "Category 1",
        "slug": "category-1",
        "count": 1
    },
    {
        "categoryId": "aaaafffd",
        "name": "Category 2",
        "slug": "category-2",
        "count": 2
    },
    {
        "categoryId": "aaaafffc",
        "name": "Category 3",
        "slug": "category-3",
        "count": 2
    },
    {
        "categoryId": "aaaafffb",
        "name": "Category 4",
        "slug": "category-4",
        "count": 1
    },
    {
        "categoryId": "aaaafffe",
        "name": "Category 5",
        "slug": "category-5",
        "count": 1
    }
}

是否有任何查询可以实现这一目标?先感谢您。也可以不更改映射吗?因为我无法更改映射(它来自管理)。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    terms aggregation 应该是 keyword 类型的字段或任何其他适用于桶聚合的数据类型。要与text 一起使用,您需要启用fielddata

    了解更多请参考本官方documentation

    使用索引映射和搜索查询添加工作示例

    映射

    "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "categories" : {
          "type" : "nested",
          "properties" : {
            "categoryId" : {
              "type" : "text"
            },
            "name" : {
              "type" : "text"
            },
            "slug" : {
              "type" : "keyword"    <-- change data type to keyword
            }
          }
        }
      }
    }
    

    搜索查询

        {
        "aggs": {
            "count": {
                "nested": {
                    "path": "categories"
                },
                "aggs": {
                    "categories_count": {
                        "terms": {
                            "field": "categories.slug"
                        }
                    }
                }
            }
        }
     }
    

    搜索结果:

        "aggregations": {
        "count": {
          "doc_count": 7,
          "categories_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "category-2",
                "doc_count": 2
              },
              {
                "key": "category-3",
                "doc_count": 2
              },
              {
                "key": "category-1",
                "doc_count": 1
              },
              {
                "key": "category-4",
                "doc_count": 1
              },
              {
                "key": "category-5",
                "doc_count": 1
              }
            ]
          }
        }
      }
    

    【讨论】:

    • 我知道关键字解决方案。不幸的是,映射似乎来自高层管理,所以我无法更改它。有没有办法不将其更改为关键字?另外,如何显示存储桶内的其他字段?我还想显示名称和 CategoryId。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2017-05-31
    相关资源
    最近更新 更多