elasticsearch - 如何使用Elasticsearch方面查询对结果进行分组

标签 elasticsearch elasticsearch-query

我有以下格式的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" }
}

我想计算汽车数量和相应的颜色。我正在使用elasticsearch方面来执行此操作。

我的查询
$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 } ]

我想对结果进行分组。是否可以在elasticsearch查询中做到这一点。

提前致谢

最佳答案

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

刻面已弃用,并将很快从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
                     }
                  ]
               }
            }
         ]
      }
   }

关于elasticsearch - 如何使用Elasticsearch方面查询对结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32604014/

相关文章:

elasticsearch - 动态模板 : store object as a plain string

elasticsearch - elasticsearch 是否支持条件查询?

elasticsearch - Elasticsearch建议功能的前缀为like

elasticsearch - 当涉及特殊字符时,Elasticsearch查询过滤器/术语不起作用

elasticsearch - Kibana绘制图表只是度量的差异而不是总数

elasticsearch - Elasticsearch 增加权重或增强查询和过滤器中的多个术语子句

spring - 索引排序Elasticsearch

elasticsearch - Elasticsearch日期查询。在某个月出生的人

java - 我无法在 elasticsearch 中将地理点设置为 null

sorting - 按属性对Elasticsearch连续结果进行自定义排序