Elasticsearch - 对同一嵌套范围内的多个字段进行聚合

Red*_*pia 5 aggregation elasticsearch

我正在按标签聚合产品搜索结果,标签有名称和 ID 字段。如何将两个字段都返回到聚合存储桶中?我可以得到一个,但我不知道如何得到两个。顺便说一句,我的集群上的脚本访问已关闭,因此我无法使用它。

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

"mappings": {
    "products": {
        "properties": { 
            "title": {
                "type": "string"
            },
            "description": {
                "type": "string"
            },
            "topics": {
                "properties": {
                    "id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "name": {
                        "type" : "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的查询:

"query": {
    "multi_match": {
        "query": "Testing 1 2 3",
        "fields": ["title", "description"]
    },
    "aggs": {
        "Topics": {
            "terms": {
                "field": "topics.id",
                "size": 15
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的聚合桶如下所示:

在此处输入图片说明

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

Rah*_*hul 5

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

"topics": {
    "type": "nested",
    "properties": {
        "id": {
            "type": "string",
            "index": "not_analyzed"
        },
        "name": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于多个字段的聚合,需要使用子聚合

这是一个示例聚合查询:

 {
      "aggs": {
        "topics_agg": {
          "nested": {
            "path": "topics"
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "topics.id"
              },
              "aggs": {
                "name": {
                  "terms": {
                    "field": "topics.name"
                  }
                }
              }
            }
          }
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

聚合样本结果:

    "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
                            }
                         ]
                      }
                   },
..............
Run Code Online (Sandbox Code Playgroud)

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

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