用于嵌套聚合以返回计数值桶的弹性搜索查询是什么?

时间:2020-07-18 11:42:57

标签: elasticsearch elasticsearch-query

我在Elastic Search中有个别客户的数据,其对Food_Item的喜欢程度存储如下所示。客户喜欢许多“ Food_Items”。因此,它的清单。我也有很多顾客。

我的数据格式如下:

{
   "id": 1,
   "customerName":"John",
   "likings":[
        {
           "Food_Item": "Pizza",
           "OnAScaleOfTen": 9
        },
        {
           "Food_Item": "Chinese",
           "OnAScaleOfTen": 10
        }
   ]
},

{
   "id": 2,
   "customerName":"Mary",
   "likings":[
        {
           "Food_Item": "Burger",
           "OnAScaleOfTen": 10
        },
        {
           "Food_Item": "Chinese",
           "OnAScaleOfTen": 6
        }
   ]
}

现在,如果我想在 AGGR 结果中列出唯一的“ Food_Items”及其对应的计数,如下所示:

"Liking_Status": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Chinese",
                    "Liking Count": {
                        "value": 2
                    }
                },
                {
                    "key": "Pizza",
                    "Liking Count": {
                        "value": 1
                    }
                },
                {
                    "key": "Burger",
                    "Liking Count": {
                        "value": 1
                    }
                }]}

我对索引的映射是:

{

    "mappings": {
        "doc": {
            "properties": {
                "customerName": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "long"
                },
                "likings": {
        "type":"nested",
                    "properties": {
                        "Food_Item": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "OnAScaleOfTen": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}

任何人都可以通过Elastic Search Query帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

您需要的是nested aggregation

{
  "size": 0,
  "aggs": {
    "buckets": { //aggregating on nested field
      "nested": {
        "path": "likings"
      },
      "aggs": {
        "liking_count": {//term aggregation on the obj
          "terms": {
            "field": "likings.Food_Item.keyword"
          }
        }
      }
    }
  }
}

映射:

我刚才提到likings是嵌套的。除其他默认设置。在这种情况下,Food_Item是文本。术语aggs适用于关键字。因此从索引中使用了它的关键字版本。

输出:

"aggregations": {
        "buckets": {
            "doc_count": 4,
            "liking_count": { //You can name what you want here
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Chinese",
                        "doc_count": 2
                    },
                    {
                        "key": "Burger",
                        "doc_count": 1
                    },
                    {
                        "key": "Pizza",
                        "doc_count": 1
                    }
                ]
            }
        }
    }
相关问题