gpt4 book ai didi

elasticsearch - 用于嵌套聚合以返回计数值桶的 Elasticsearch 查询是什么?

转载 作者:行者123 更新时间:2023-12-03 01:14:59 24 4
gpt4 key购买 nike

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

{
"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
}
]
}
现在,如果我要在 list 中列出唯一的“Food_Items”及其对应的计数,则在 AGGR 结果中像这样:
"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"
}
}
}
}
}
}
}
谁能帮助我进行 flex 搜索查询。谢谢。

最佳答案

您需要的是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
}
]
}
}
}

关于elasticsearch - 用于嵌套聚合以返回计数值桶的 Elasticsearch 查询是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62968139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com