Elasticsearch - 在同一嵌套范围内聚合多个字段

Elasticsearch - Aggregation on multiple fields in the same nested scope

我按标签汇总产品搜索结果,这些标签有名称和 ID 字段。如何将两个字段都返回到聚合存储桶中?我可以得到一个或另一个,但我无法弄清楚如何同时获得两者。 顺便说一句,我的集群上的脚本访问已关闭,所以我无法使用它。

这是我的产品映射(针对此问题进行了简化):

"mappings": {
    "products": {
        "properties": { 
            "title": {
                "type": "string"
            },
            "description": {
                "type": "string"
            },
            "topics": {
                "properties": {
                    "id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "name": {
                        "type" : "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

这是我的查询:

"query": {
    "multi_match": {
        "query": "Testing 1 2 3",
        "fields": ["title", "description"]
    },
    "aggs": {
        "Topics": {
            "terms": {
                "field": "topics.id",
                "size": 15
            }
        }
    }
}

我的聚合桶是这样的:

...第一个桶中的 "key" 值是 topics.id 字段值。有没有办法将我的 topics.name 字段添加到存储桶中?

如果您想在您的存储桶中添加另一个字段作为键,则 (id,name) 将充当唯一的存储桶。您需要 id 和名称之间的关联。如果没有嵌套映射,id 和名称列表是单独的数组。因此,您需要将其映射为嵌套。

"topics": {
    "type": "nested",
    "properties": {
        "id": {
            "type": "string",
            "index": "not_analyzed"
        },
        "name": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
}

对于多个字段的聚合,您需要使用sub-aggregations

这是一个聚合查询示例:

 {
      "aggs": {
        "topics_agg": {
          "nested": {
            "path": "topics"
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "topics.id"
              },
              "aggs": {
                "name": {
                  "terms": {
                    "field": "topics.name"
                  }
                }
              }
            }
          }
        }
      }
    }

聚合样本结果:

    "aggregations": {
          "topics_agg": {
             "doc_count": 5,
             "name": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                   {
                      "id": 123,
                      "doc_count": 6,
                      "name": {
                         "doc_count_error_upper_bound": 0,
                         "sum_other_doc_count": 0,
                         "buckets": [
                            {
                               "key": "topic1",
                               "doc_count": 3
                            },
                            {
                               "key": "topic2",
                               "doc_count": 3
                            }
                         ]
                      }
                   },
                   {
                      "key": 456,
                      "doc_count": 2,
                      "name": {
                         "doc_count_error_upper_bound": 0,
                         "sum_other_doc_count": 0,
                         "buckets": [
                            {
                               "key": "topic1",
                               "doc_count": 2
                            }
                         ]
                      }
                   },
..............

注意:对于 id : 123,有多个名称桶。因为同一个 ID 有多个名称值。要创建单独的唯一存储桶,只需创建所有父子组合。

例如。 123-topic1, 123-topic2, 456-topic1