首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch组/按搜索条件聚合响应

Elasticsearch组/按搜索条件聚合响应
EN

Stack Overflow用户
提问于 2020-10-04 05:16:07
回答 2查看 87关注 0票数 1

我有一个产品,它有一个属性categoryIds。

代码语言:javascript
复制
"id" : 1,
"title" : "product",
"price" : "1100.00",
"categories" : [ the ids of the product's categories],
"tags" : [ the ids of the product's tags ],
"variants" : [ nested type with properties: name, definition, maybe in the future availability dates]

我想根据查询中的类别对产品id进行分组。在POST _search中,我询问属于特定类别的产品(例如1,2,3),我也可以用变体限制它们。如何对我的答案进行分组/聚合,以获得类别的productIds列表?我想要得到的是:

代码语言:javascript
复制
{
    "productsForCategories": {
        "1": [
            "product-1",
            "product-2",
            "product-3"
        ],
        "2": [
            "product-1",
            "product-3",
            "product-4"
        ],
        "3": [
            "product-5",
            "product-6"
        ]
    }
}

提前感谢你的所有回答。

java生成了什么。

代码语言:javascript
复制
curl --location --request POST 'https://localhost:9200/products/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "categories": {
                                        "value": 7,
                                        "boost": 1.0
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0,
                        "_name": "fromRawQuery"
                    }
                }
            ],
            "filter": [
                {
                    "bool": {
                        "adjust_pure_negative": true,
                        "boost": 1.0,
                        "_name": "filterPart"
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0,
            "_name": "queryPart"
        }
    },
    "_source": {
        "includes": [
            "categories",
            "productType",
            "relations"
        ],
        "excludes": []
    },
    "stored_fields": "_id",
    "sort": [
        {
            "_score": {
                "order": "desc"
            }
        }
    ],
    "aggregations": {
        "agg": {
            "global": {},
            "aggregations": {
                "categories": {
                    "terms": {
                        "field": "categories",
                        "size": 2147483647,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "productsForCategories": {
                            "terms": {
                                "field": "_id",
                                "size": 2147483647,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [
                                    {
                                        "_count": "desc"
                                    },
                                    {
                                        "_key": "asc"
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}'```
EN

回答 2

Stack Overflow用户

发布于 2020-10-04 11:23:31

您可以使用terms aggregation,它是一个基于多存储桶值源的聚合,其中存储桶是动态构建的-每个唯一值一个。

添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

代码语言:javascript
复制
{
  "mappings":{
    "properties":{
      "categories":{
        "type":"keyword"
      }
    }
  }
}

索引数据:

代码语言:javascript
复制
{
  "id":1,
  "product":"p1",
  "category":[1,2,7]
}
{
  "id":2,
  "product":"p2",
  "category":[7,4,5]
}
{
  "id":3,
  "product":"p3",
  "category":[4,5,6]
}    

搜索查询:

代码语言:javascript
复制
    {
  "size": 0,
  "aggs": {
    "cats": {
      "terms": {
        "field": "cat_ids",
        "include": [
          7
        ]
      },
      "aggs": {
        "products": {
          "terms": {
            "field": "product.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

搜索结果:

代码语言:javascript
复制
"aggregations": {
    "cats": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 7,
          "doc_count": 2,
          "products": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "p1",
                "doc_count": 1
              },
              {
                "key": "p2",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
票数 0
EN

Stack Overflow用户

发布于 2020-10-04 14:31:14

我相信你想要的是与每个类别相对应的产品。正如Bhavya提到的,您可以使用term聚合来实现同样的功能。

代码语言:javascript
复制
GET products/_search
{
  "size": 0, //<===== If you need only aggregated results, set this to 0. It represents query result size.
  "aggs": {
    "categories": {
      "terms": {
        "field": "cat_ids", // <================= Equivalent of group by Cat_ids
        "size": 10
      },"aggs": {
        "products": {
          "terms": {
            "field": "name.keyword",//<============= For Each category group by products
            "size": 10
          }
        }
      }
    }
  }
}

结果:

代码语言:javascript
复制
  "aggregations" : {
"categories" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : 1,       //<========== category id
      "doc_count" : 2, //<========== For the given category id 2 products
      "products" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "p1",  //<========= for cat_id=1, p1 is there
            "doc_count" : 1
          },
          {
            "key" : "p2", //<========= for cat_id=1, p2 is there
            "doc_count" : 1
          }
        ]
      }
    },
    {
      "key" : 2,
      "doc_count" : 2,
      "products" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "p1",
            "doc_count" : 1
          },
          {
            "key" : "p2",
            "doc_count" : 1
          }
        ]
      }
    },
    {
      "key" : 3,
      "doc_count" : 1,
      "products" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "p1",
            "doc_count" : 1
          }
        ]
      }
    }
  ]
}

}

详细信息以注释的形式提供。请删除注释,然后尝试运行查询。

过滤聚合结果: See this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64189083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档