弹性搜索:根据子记录中的字段总和对父记录进行排序

问题描述 投票:0回答:1

我按照以下格式以父/子关系将数据存储在 Elasticsearch 中:

映射

{
  "mappings": {
     "properties": {
      "product_id": {
        "type": "keyword",
        "norms": false
      },
      "product_name": {
        "type": "text",
        "norms": false
      },
      "category_id": {
        "type": "keyword",
        "norms": false
      },
      "type": {
         "type": "keyword"
      },
      "join_field": {
        "type": "join",
        "eager_global_ordinals": true,
        "relations": {
          "product": "review"
        }
      },
      "ratings": {
          "type": "double"
      },
      "year": {
          "type": "integer"
      }
    }
  }
}

家长记录 - 产品

{
   product_id: 123,
   product_name: "Hello",
   category_id: 456,
   type: "XYZ"
}

儿童记录 - 评论

{
  "product_id": "123",
  "ratings": 5,
  "year": 2024 
}

我想获取特定category_id的所有产品,并且该产品应按特定年份的所有评论的总和降序排序。

elasticsearch elastic-stack
1个回答
0
投票

我使用示例文档。产品位于 ID 为 1、4、7 的文档中

PUT /products_reviews/_bulk?routing=1&refresh
{"create":{"_id":1}}
{"product_id":123,"product_name":"Hello","category_id":456,"type":"XYZ","join_field":"product"}
{"create":{"_id":2}}
{"product_id":123,"ratings":5,"year":2024,"join_field":{"name":"review","parent":1}}
{"create":{"_id":3}}
{"product_id":123,"ratings":3,"year":2024,"join_field":{"name":"review","parent":1}}
{"create":{"_id":4}}
{"product_id":124,"product_name":"Bye","category_id":456,"type":"XYZ","join_field":"product"}
{"create":{"_id":5}}
{"product_id":124,"ratings":1,"year":2024,"join_field":{"name":"review","parent":4}}
{"create":{"_id":6}}
{"product_id":124,"ratings":2,"year":2024,"join_field":{"name":"review","parent":4}}
{"create":{"_id":7}}
{"product_id":125,"product_name":"Thanks","category_id":451,"type":"XYZ","join_field":"product"}
{"create":{"_id":8}}
{"product_id":125,"ratings":2,"year":2024,"join_field":{"name":"review","parent":7}}
{"create":{"_id":9}}
{"product_id":123,"ratings":3,"year":2023,"join_field":{"name":"review","parent":1}}

具有内部聚合的聚合查询和

bucket_sort
用于对产品进行排序

POST /products_reviews/_search?filter_path=aggregations.by_product_id.buckets
{
    "query": {
        "term": {
            "category_id": {
                "value": 456
            }
        }
    },
    "aggs": {
        "by_product_id": {
            "terms": {
                "field": "product_id",
                "size": 10
            },
            "aggs": {
                "review_filter": {
                    "children": {
                        "type": "review"
                    },
                    "aggs": {
                        "year_filter": {
                            "filter": {
                                "term": {
                                    "year": 2024
                                }
                            },
                            "aggs": {
                                "sum_ratings": {
                                    "sum": {
                                        "field": "ratings"
                                    }
                                }
                            }
                        }
                    }
                },
                "sort_by_sum_ratings": {
                    "bucket_sort": {
                        "sort": [
                            {
                                "review_filter>year_filter>sum_ratings": "desc"
                            }
                        ]
                    }
                }
            }
        }
    }
}

回应

{
    "aggregations" : {
        "by_product_id" : {
            "buckets" : [
                {
                    "key" : "123",
                    "doc_count" : 1,
                    "review_filter" : {
                        "doc_count" : 3,
                        "year_filter" : {
                            "doc_count" : 2,
                            "sum_ratings" : {
                                "value" : 8.0
                            }
                        }
                    }
                },
                {
                    "key" : "124",
                    "doc_count" : 1,
                    "review_filter" : {
                        "doc_count" : 2,
                        "year_filter" : {
                            "doc_count" : 2,
                            "sum_ratings" : {
                                "value" : 3.0
                            }
                        }
                    }
                }
            ]
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.