elasticsearch-聚合结果中的其他字段

夸兹

我有以下类别的汇总

{
    "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给查询过滤器,我还不清楚如何查询聚合

安德烈·斯特凡(Andrei Stefan)

在上使用子聚合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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章