Elasticsearch嵌套聚合得到的结果太慢了

时间:2018-04-23 15:52:14

标签: elasticsearch aggregation

我将来有一个包含数十亿文档的索引,现在它的文档大约是20万个。我花了超过10秒来获得结果,而我需要查询大约3-4s的十亿个文档。我的结构是错误的还是需要改进查询或服务器配置?我正在使用亚马逊弹性搜索服务。

此查询将返回每个区域中每个站点的金额/交易/项目 查询:

{
  "size" : 0,
  "query": {
        "bool": {
            "must": 
            [
                {
                    "range": {
                        "date_sec": {
                            "gte": "1483228800",
                            "lte": "1525046400"
                        }
                    }
                },
                {
                    "range": {
                        "time_sec": {
                            "gte": "32400",
                            "lte": "75600"
                        }
                    }
                }   
            ]
        }   
    },
  "aggs": {
    "numstoreamountclient" : {
        "filter" : { "range" : { "amount" : { "gt" : 0 } } },
        "aggs": { 
                "numstore_amountclient": { 
                    "cardinality" : {
                      "field" : "id_station"
                    }
                }
            } 
    },
    "id_station": {
      "terms": {
        "field": "id_station"
      },
      "aggs": {
        "area_type": {
          "terms": {
            "field": "area_type"
          },
          "aggs": {
                "max_time" : { "max" : { "field" : "time_sec" } },
                "min_time" : { "min" : { "field" : "time_sec" } },
                "amountclient": {
                  "sum": {
                    "field": "amount"
                  }
                },
                "itemclient": {
                  "sum": {
                    "field": "items"
                  }
                },
                "transactionclient" : { 
                    "value_count" : 
                    { 
                        "field" : "id" 

                    } 

                },
                "inwatchinghour": {
                  "filter" : { "term" : { "in_watchinghour" :  1 } },
                  "aggs" : {
                      "amountclientwatch": {
                              "sum": {
                                "field": "amount"
                              }
                        },
                        "itemclient": {
                              "sum": {
                                "field": "items"
                              }
                        },
                        "transactionclientwatch" : { 
                            "value_count" : 
                            { 
                                "field" : "id" 
                            } 
                        }
                    }
                },
                "saleclient": {
                  "filter" : { 
                    "bool": {
                        "must": 
                        [
                            {
                                "term" : { "in_watchinghour" :  1 } 
                            },
                            {
                                "range": {
                                    "items": {
                                        "gt": "0"
                                    }
                                }
                            },
                            {
                                "range": {
                                    "amount": {
                                        "gt": "0"
                                    }
                                }
                            }
                        ]
                    }   
                  },
                  "aggs" : {
                      "sale_client" : { 
                            "value_count" : 
                            { 
                                "field" : "id" 
                            } 
                        }
                    }
                }
            }
        }
      }
    }
  }
}

结果:

{
    "took": 10757,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 19778330,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "numstoreamountclient": {
            "doc_count": 19677164,
            "numstore_amountclient": {
                "value": 35
            }
        },
        "id_station": {
            "doc_count_error_upper_bound": 437877,
            "sum_other_doc_count": 11401869,
            "buckets": [
                {
                    "key": 2209,
                    "doc_count": 1456505,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 1456505,
                                "saleclient": {
                                    "doc_count": 708499,
                                    "sale_client": {
                                        "value": 708499
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 711435,
                                    "transactionclientwatch": {
                                        "value": 711435
                                    },
                                    "amountclientwatch": {
                                        "value": 210203295816
                                    },
                                    "itemclient": {
                                        "value": 4105206
                                    }
                                },
                                "amountclient": {
                                    "value": 427392789897
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 1456505
                                },
                                "itemclient": {
                                    "value": 8402911
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2210,
                    "doc_count": 890590,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 890590,
                                "saleclient": {
                                    "doc_count": 357520,
                                    "sale_client": {
                                        "value": 357520
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 358900,
                                    "transactionclientwatch": {
                                        "value": 358900
                                    },
                                    "amountclientwatch": {
                                        "value": 89792941442
                                    },
                                    "itemclient": {
                                        "value": 2146312
                                    }
                                },
                                "amountclient": {
                                    "value": 222577251265
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 890590
                                },
                                "itemclient": {
                                    "value": 5346273
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2226,
                    "doc_count": 844491,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 844491,
                                "saleclient": {
                                    "doc_count": 346801,
                                    "sale_client": {
                                        "value": 346801
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 347730,
                                    "transactionclientwatch": {
                                        "value": 347730
                                    },
                                    "amountclientwatch": {
                                        "value": 90585228756
                                    },
                                    "itemclient": {
                                        "value": 1817412
                                    }
                                },
                                "amountclient": {
                                    "value": 219008246857
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 844491
                                },
                                "itemclient": {
                                    "value": 4409412
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2317,
                    "doc_count": 812409,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 812409,
                                "saleclient": {
                                    "doc_count": 292933,
                                    "sale_client": {
                                        "value": 292933
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 294866,
                                    "transactionclientwatch": {
                                        "value": 294866
                                    },
                                    "amountclientwatch": {
                                        "value": 105661613404
                                    },
                                    "itemclient": {
                                        "value": 2144352
                                    }
                                },
                                "amountclient": {
                                    "value": 290725384084
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 812409
                                },
                                "itemclient": {
                                    "value": 5925558
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2211,
                    "doc_count": 811198,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 811198,
                                "saleclient": {
                                    "doc_count": 262617,
                                    "sale_client": {
                                        "value": 262617
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 265515,
                                    "transactionclientwatch": {
                                        "value": 265515
                                    },
                                    "amountclientwatch": {
                                        "value": 70763222934
                                    },
                                    "itemclient": {
                                        "value": 1783073
                                    }
                                },
                                "amountclient": {
                                    "value": 213071496626
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 811198
                                },
                                "itemclient": {
                                    "value": 5476443
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2331,
                    "doc_count": 806670,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 806670,
                                "saleclient": {
                                    "doc_count": 349472,
                                    "sale_client": {
                                        "value": 349472
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 350285,
                                    "transactionclientwatch": {
                                        "value": 350285
                                    },
                                    "amountclientwatch": {
                                        "value": 82784018110
                                    },
                                    "itemclient": {
                                        "value": 2079211
                                    }
                                },
                                "amountclient": {
                                    "value": 192804137579
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 806670
                                },
                                "itemclient": {
                                    "value": 4834069
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2323,
                    "doc_count": 749161,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 749161,
                                "saleclient": {
                                    "doc_count": 280928,
                                    "sale_client": {
                                        "value": 280928
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 282498,
                                    "transactionclientwatch": {
                                        "value": 282498
                                    },
                                    "amountclientwatch": {
                                        "value": 62082735118
                                    },
                                    "itemclient": {
                                        "value": 1588445
                                    }
                                },
                                "amountclient": {
                                    "value": 162365212278
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 749161
                                },
                                "itemclient": {
                                    "value": 4231490
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2345,
                    "doc_count": 727589,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 727589,
                                "saleclient": {
                                    "doc_count": 340141,
                                    "sale_client": {
                                        "value": 340141
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 341590,
                                    "transactionclientwatch": {
                                        "value": 341590
                                    },
                                    "amountclientwatch": {
                                        "value": 107492036777
                                    },
                                    "itemclient": {
                                        "value": 2421158
                                    }
                                },
                                "amountclient": {
                                    "value": 228611232646
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 727589
                                },
                                "itemclient": {
                                    "value": 5138628
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2329,
                    "doc_count": 663856,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 663856,
                                "saleclient": {
                                    "doc_count": 163358,
                                    "sale_client": {
                                        "value": 163358
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 164339,
                                    "transactionclientwatch": {
                                        "value": 164339
                                    },
                                    "amountclientwatch": {
                                        "value": 55298080357
                                    },
                                    "itemclient": {
                                        "value": 1209514
                                    }
                                },
                                "amountclient": {
                                    "value": 211070998632
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 663856
                                },
                                "itemclient": {
                                    "value": 4875689
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                },
                {
                    "key": 2355,
                    "doc_count": 613992,
                    "area_type": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 613992,
                                "saleclient": {
                                    "doc_count": 113575,
                                    "sale_client": {
                                        "value": 113575
                                    }
                                },
                                "inwatchinghour": {
                                    "doc_count": 114038,
                                    "transactionclientwatch": {
                                        "value": 114038
                                    },
                                    "amountclientwatch": {
                                        "value": 30494132488
                                    },
                                    "itemclient": {
                                        "value": 563628
                                    }
                                },
                                "amountclient": {
                                    "value": 140705052880
                                },
                                "max_time": {
                                    "value": 75600
                                },
                                "transactionclient": {
                                    "value": 613992
                                },
                                "itemclient": {
                                    "value": 2920908
                                },
                                "min_time": {
                                    "value": 32400
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

enter image description here

0 个答案:

没有答案