0

我有以下格式的 json 数据

{
"ID": { "Color": "Black", "Product": "Car" },
"ID": { "Color": "Black", "Product": "Car" },
"ID": { "Color": "Black", "Product": "Van" },
"ID": { "Color": "Black", "Product": "Van" },
"ID": { "Color": "Ash", "Product": "Bike" }
}

我想计算汽车的数量和相应的颜色。我正在使用弹性搜索方面来做到这一点。

我的查询

$http.post('http://localhost:9200/product/productinfoinfo/_search?size=5', { "aggregations": { "ProductInfo": { "terms": { "field": "product" } } }, "facets": { "ProductColor": { "terms": { "field": "Color", "size": 10 } } } })

我得到如下输出

"facets": { "ProductColor": { "_type": "terms", "missing": 0, "total": 7115, "other": 1448, "terms": [ { "term": "Black", "count": 4 }, { "term": "Ash","count":1} }, 
"aggregations": { "ProductInfo": { "doc_count_error_upper_bound": 94, "sum_other_doc_count": 11414, "buckets": [ { "key": "Car", "doc_count": 2 }, { "key": "Van", "doc_count": 2 }, { "key": "Bike", "doc_count": 1 } ] } } } 

我真正想要的是,

[ { "key": "Car", "doc_count": 2, "Color":"Black", "count":2 }, { "key": "Van", "doc_count": 2,"Color":"Black", "count":2 }, { "key": "Bike", "doc_count": 1,"Color":"Ash", "count":1 } ]

我想按结果分组。是否可以在弹性搜索查询中做到这一点。

提前致谢

4

1 回答 1

2

这是因为您同时使用聚合和构面,如果它们相似,则不应一起使用。

Facets 已被弃用,并将很快从 ElasticSearch 中删除。聚合是进行类似“分组”的查询的方法。

您只需terms在第一个聚合中嵌套另一个聚合,如下所示:

{
  "aggs": {
    "By_type": {
      "terms": {
        "field": "Product"
      },
      "aggs": {
        "By_color": {
          "terms": {
            "field": "Color"
          }
        }
      }
    }
  }
}

结果将接近您想要的:

"aggregations": {
      "By_type": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "bike",
               "doc_count": 2,
               "By_color": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "ash",
                        "doc_count": 1
                     },
                     {
                        "key": "black",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "car",
               "doc_count": 2,
               "By_color": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "black",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "van",
               "doc_count": 1,
               "By_color": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "black",
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }
于 2015-09-16T09:21:04.547 回答