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

时间:2016-03-17 02:54:51

标签: elasticsearch aggregation

我按标签聚合产品搜索结果,标签有名称和ID字段。如何将两个字段都放回聚合桶中?我可以得到一个或另一个,但我无法弄清楚如何获得两者。 BTW,我的群集上的脚本访问权限已关闭,因此我无法使用该功能。

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

videotestsrc

这是我的疑问:

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

我的聚合桶看起来像这样:

enter image description here

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

1 个答案:

答案 0 :(得分:4)

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

主题“:{                “类型”:“嵌套”,                 “properties”:{                     “ID”: {                         “type”:“string”,                         “index”:“not_analyzed”                     },                     “名称”: {                         “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

相关问题