elasticsearch 基于多重聚集的弹性聚类搜索排序

r8uurelv  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(219)

我尝试用不同的聚集标准来获得我的数据,然后我想根据其中一个聚集标准来对它进行排序。在这个特定的例子中,我想根据“Monthly_Income/ SUM”标准来对我的数据进行降序排序。
我搜索和尝试了很多东西,但没有一个对我有效。你能给予我答案吗,因为我是新的ElasticSearch。
到目前为止我搜索了什么,但无法解决问题;“ordering_by_a_sub_aggregation,基于“深度”度量进行排序,搜索聚合存储桶术语聚合脚本,搜索聚合存储桶多术语聚合
把问题形象化。我总是得到下面的结果,然而我尝试了很多方法,但我不能达到预期的结果。

非预期结果x1c 0d1x
预期结果

请求

`

{
    "query": {
        "bool": {
            "must": [],
            "must_not": []
        }
    },
    "size": 0,
    "aggs": {
        "GENDER": {
            "terms": {
                "field": "GENDER.keyword",
                "size": 10000000,
                "missing": "N/A"
           // ,"order": {"MARTIAL_STATUS>Monthly_Income_0.max" : "desc" }  
            },
            "aggs": {
                "MARTIAL_STATUS": {
                    "terms": {
                        "field": "MARTIAL_STATUS.keyword",
                        "size": 10000000,
                        "missing": "N/A"      
                     //   ,"order": {"Monthly_Income_0.value" : "desc" }                 
                    },
                    "aggs": {
                        "Monthly_Income_0": {
                            "sum": {
                                "field": "Monthly_Income"
                            }
                        },
                        "Monthly_Income_1": {
                            "value_count": {
                                "field": "Monthly_Income"
                            }
                        },
                        "SALE_PRICE_2": {
                            "sum": {
                                "field": "SALE_PRICE"
                            }
                        }
                    // ,"sort_by_percentage": {
                    //     "bucket_sort": {
                    //         "sort": [ { "Monthly_Income_0.value": { "order": "desc"   }         }     ]
                    //     }
                    //     }
                     }
                }
            }
        }
    }
}

`

回应

`

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "GENDER": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Male",
                    "doc_count": 40959,
                    "MARTIAL_STATUS": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Married",
                                "doc_count": 35559,
                                "SALE_PRICE_2": {
                                    "value": 2.530239767013672E9
                                },
                                "Monthly_Income_0": {
                                    "value": 3.59618565E8
                                },
                                "Monthly_Income_1": {
                                    "value": 35559
                                }
                            },
                            {
                                "key": "Single",
                                "doc_count": 5399,
                                "SALE_PRICE_2": {
                                    "value": 3.7742297754296875E8
                                },
                                "Monthly_Income_0": {
                                    "value": 5.3465554E7
                                },
                                "Monthly_Income_1": {
                                    "value": 5399
                                }
                            },
                            {
                                "key": "N/A",
                                "doc_count": 1,
                                "SALE_PRICE_2": {
                                    "value": 87344.203125
                                },
                                "Monthly_Income_0": {
                                    "value": 40000.0
                                },
                                "Monthly_Income_1": {
                                    "value": 1
                                }
                            }
                        ]
                    }
                },
                {
                    "key": "Female",
                    "doc_count": 7777,
                    "MARTIAL_STATUS": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Married",
                                "doc_count": 5299,
                                "SALE_PRICE_2": {
                                    "value": 3.9976638293359375E8
                                },
                                "Monthly_Income_0": {
                                    "value": 4.4994796E7
                                },
                                "Monthly_Income_1": {
                                    "value": 5299
                                }
                            },
                            {
                                "key": "Single",
                                "doc_count": 2477,
                                "SALE_PRICE_2": {
                                    "value": 1.8698677312695312E8
                                },
                                "Monthly_Income_0": {
                                    "value": 1.8793502E7
                                },
                                "Monthly_Income_1": {
                                    "value": 2477
                                }
                            },
                            {
                                "key": "N/A",
                                "doc_count": 1,
                                "SALE_PRICE_2": {
                                    "value": 101006.8203125
                                },
                                "Monthly_Income_0": {
                                    "value": 10000.0
                                },
                                "Monthly_Income_1": {
                                    "value": 1
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

`
我尝试根据聚合列进行订购,但无法实现

bxgwgixi

bxgwgixi1#

我对你的问题的理解是,你想按性别和婚姻状况的组合分组
我已经使用runtime mapping连接字段“gender”和婚姻状况,并使用term aggregation对运行时字段进行分组,并基于sum对组进行排序。

{
  "size": 0, 
  "runtime_mappings": {
    "gender-maritalstatus": {
      "type": "keyword",
      "script": {
        "source": """
                 def gender='NA';
                 def maritalstatus='NA';
                 
                 if(doc['Gender.keyword'].size()!=0)
                      gender= doc['Gender.keyword'].value;
                 if(doc['Marital_Status.keyword'].size()!=0)
                      maritalstatus= doc['Marital_Status.keyword'].value;
                      
                 emit(gender+'-'+maritalstatus);
                """
      }
    }
  },
  "aggs": {
    "gender-marital-grouping": {
      "terms": {
        "field": "gender-maritalstatus",
        "order": {
          "monthly_income": "desc"
        }, 
        "size": 10
      },
      "aggs": {
        "monthly_income": {
          "sum": {
            "field": "Monthly_Income"
          }
        }
      }
    }
  }
}

结果

"buckets" : [
        {
          "key" : "Female-Single",
          "doc_count" : 2,
          "monthly_income" : {
            "value" : 300.0
          }
        },
        {
          "key" : "Male-Married",
          "doc_count" : 2,
          "monthly_income" : {
            "value" : 200.0
          }
        },
        {
          "key" : "Female-NA",
          "doc_count" : 1,
          "monthly_income" : {
            "value" : 100.0
          }
        },
        {
          "key" : "Male-NA",
          "doc_count" : 1,
          "monthly_income" : {
            "value" : 100.0
          }
        },
        {
          "key" : "Male-Single",
          "doc_count" : 1,
          "monthly_income" : {
            "value" : 100.0
          }
        }
      ]

相关问题