我有一个产品有一个属性类别ID。
"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\u搜索中,我询问属于特定类别(例如[1,2,3])的产品,我还可以用一个变体来限制它们。如何对答案进行分组/聚合以获得类别的productID列表?我想要的是:
{
"productsForCategories": {
"1": [
"product-1",
"product-2",
"product-3"
],
"2": [
"product-1",
"product-3",
"product-4"
],
"3": [
"product-5",
"product-6"
]
}
}
提前谢谢你的回答。
java生成了什么。
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"
}
]
}
}
}
}
}
}
}
}'```
2条答案
按热度按时间svmlkihl1#
我相信你想要的是与每一类产品相对应的产品。正如bhavya提到的,您可以使用术语聚合来表示相同的结果。
结果:
}
详情以评论形式提供。请删除注解并尝试运行查询。
过滤聚合results:see this
z6psavjg2#
您可以使用术语聚合,它是一种基于源代码的多bucket值聚合,其中动态构建bucket—每个惟一值一个bucket。
添加索引数据、Map、搜索查询和搜索结果的工作示例
索引Map:
索引数据:
搜索查询:
搜索结果: