elasticsearch - 聚合结果中的附加字段

时间:2015-05-14 11:20:20

标签: elasticsearch faceted-search

我对 Categories

有以下聚合
{
    "aggs": {
        "category": {
            "terms": { "field": "category.name" }
        }
    }
}

// results
"category": {
    "buckets": [
        {
            "key": "computer & office",
            "doc_count": 365
        },
        {
            "key": "home & garden",
            "doc_count": 171
        },
        {
            "key": "consumer electronics",
            "doc_count": 49
        },
    ]
}

如何将其他字段(如category.id)传递给类别存储区,因此我可以category.id查询用户是否单击了某个聚合。我不清楚如何查询聚合,如果有任何直接方式,或者您必须创建新查询并将存储桶key传递给查询过滤器。

2 个答案:

答案 0 :(得分:2)

category.id上使用子聚合,在查看结果时你会做更多的工作,但我认为它比改变映射更好:

{
  "aggs": {
    "name": {
      "terms": {
        "field": "name"
      },
      "aggs": {
        "id": {
          "terms": {
            "field": "id"
          }
        }
      }
    }
  }
}

结果将如下所示:

   "aggregations": {
      "name": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "consumer electronics",
               "doc_count": 2,
               "id": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": 2,
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "computer & office",
               "doc_count": 1,
               "id": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": 5,
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "home & garden",
               "doc_count": 1,
               "id": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": 1,
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "whatever",
               "doc_count": 1,
               "id": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": 3,
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }

您仍然会有类别名称,但现在您也可以将第二个聚合中的id作为根存储桶中的子存储桶:

               "key": "consumer electronics",
               ...
               "id": {
                  ...
                  "buckets": [
                     {
                        "key": 2,
                        "doc_count": 2

答案 1 :(得分:1)

您可以添加子聚合:

{
    "aggs": {
        "category": {
            "terms": { 
                 field": "category.name",
                 "aggs": {
                     "id": {
                         "terms": { "field": "category.id" }
                     }
                 }
             }
        }
    }
}

这样每个category.name存储桶将包含一个包含该类别ID的存储桶。