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

标签 elasticsearch faceted-search

我有以下类别的汇总

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

最佳答案

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

关于elasticsearch - elasticsearch-聚合结果中的其他字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30236061/

相关文章:

php - elasticsearch中 "union"的子查询

asynchronous - Elasticsearch批量更新,然后进行搜索

elasticsearch - 通过 Elasticsearch 中的查询进行文档计数聚合(如 solr 中的facet.query)

indexing - elasticsearch 中用于计算索引和类型命中的 Facet

java - Elasticsearch 前缀过滤器

elasticsearch - elasticsearch模板-从index_pattern创建别名

elasticsearch - 我们可以四舍五入 Elasticsearch 中的分数吗

python - 何时需要Elasticsearch进行数据分析和客户服务

azure - Microsoft.Azure.Search(sdk v3.0.3)未正确返回所有方面