按搜索条件列出的elasticsearch组/聚合响应

rslzwgfq  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(2)|浏览(412)

我有一个产品有一个属性类别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"
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}'```
svmlkihl

svmlkihl1#

我相信你想要的是与每一类产品相对应的产品。正如bhavya提到的,您可以使用术语聚合来表示相同的结果。

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
          }
        }
      }
    }
  }
}

结果:

"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
          }
        ]
      }
    }
  ]
}

}
详情以评论形式提供。请删除注解并尝试运行查询。
过滤聚合results:see this

z6psavjg

z6psavjg2#

您可以使用术语聚合,它是一种基于源代码的多bucket值聚合,其中动态构建bucket—每个惟一值一个bucket。
添加索引数据、Map、搜索查询和搜索结果的工作示例
索引Map:

{
  "mappings":{
    "properties":{
      "categories":{
        "type":"keyword"
      }
    }
  }
}

索引数据:

{
  "id":1,
  "product":"p1",
  "category":[1,2,7]
}
{
  "id":2,
  "product":"p2",
  "category":[7,4,5]
}
{
  "id":3,
  "product":"p3",
  "category":[4,5,6]
}

搜索查询:

{
  "size": 0,
  "aggs": {
    "cats": {
      "terms": {
        "field": "cat_ids",
        "include": [
          7
        ]
      },
      "aggs": {
        "products": {
          "terms": {
            "field": "product.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

搜索结果:

"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
              }
            ]
          }
        }
      ]
    }

相关问题