如何按度量标准结果对聚合桶进行排序?

时间:2016-06-29 03:09:06

标签: elasticsearch elasticsearch-aggregation

基于official document,似乎只能对单桶聚合进行排序。 如何在桶数聚合中按'avg'对结果进行排序?

我在'result_1'中添加'order'仍然无效。 它显示以下错误:

TransportError(500, u'search_phase_execution_exception')
Invalid terms aggregation order path [reverse>condi_range>statistic_score.avg]. 
Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. 
Sub-path [reverse>condi_range] points to non single-bucket aggregation')

关键是我必须限制'float_field'的范围,range aggregation是另一个桶聚合。

有没有人有任何想法?

谢谢!

代码示例:

{
  "aggs": {
    "condi_nested": {
      "aggs": {
        "filtered": {
          "filter": {
            "bool": {}
          },
          "aggs": {
            "result_1": {
              "terms": {
                "field": "Foo",
                "size": 100000000,
                "order": {
                  "reverse>condi_range>statistic_score.avg": "desc"
                }
              },
              "aggs": {
                "reverse": {
                  "aggs": {
                    "condi_range": {
                      "range": {
                        "ranges": [
                          {
                            "to": 100,
                            "from": 0
                          }
                        ],
                        "field": "float_field"
                      },
                      "aggs": {
                        "percentile_score": {
                          "percentiles": {
                            "field": "float_field"
                          }
                        },
                        "statistic_score": {
                          "extended_stats": {
                            "field": "float_field"
                          }
                        }
                      }
                    }
                  },
                  "reverse_nested": {}
                }
              }
            }
          }
        }
      },
      "nested": {
        "path": "nested_path_of_Foo"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我只是想出问题所在。

使用'过滤器'代替'范围'

{
  "aggs": {
    "condi_range": {
      "filter": {
        "range": {
          "float_field": {
            "gte": 0,
            "lte": 100
          }
        }
      },
      "aggs":{
        ...blahblahblah...
      }
    }
  }
}

但是在我问到这个问题后我发现了另一个问题。

我无法在嵌套聚合后对字段进行排序,引发问题中发布的相同错误。

以下是样本:

{
  "aggs": {
    "result_1": {
      "terms": {
        "field": "Foo",
        "order": {
          "condi_nested>filtered>condi_range>statistic_score.min": "desc"
        },
        "size": 100000000
      },
      "aggs": {
        "condi_nested": {
          "aggs": {
            "filtered": {
              "filter": {
                "bool": {}
              },
              "aggs": {
                "condi_range": {
                  "filter": {
                    "range": {
                      "nested_path_of_float_field.float_field": {
                        "gte": 0,
                        "lte": 100
                      }
                    }
                  },
                  "aggs": {
                    "statistic_score": {
                      "extended_stats": {
                        "field": "nested_path_of_float_field.float_field"
                      }
                    }
                  }
                }
              }
            }
          },
          "nested": {
            "path": "nested_path_of_float_field"
          }
        }
      }
    }
  }
}

和这个

{
  "aggs": {
    "condi_nested": {
      "aggs": {
        "filtered": {
          "filter": {
            "bool": {}
          },
          "aggs": {
            "result_1": {
              "terms": {
                "field": "Foo",
                "order": {
                  "reverse>condi_nested>condi_range>statistic_score.min": "desc"
                },
                "size": 100000000
              },
              "aggs": {
                "reverse": {
                  "aggs": {
                    "condi_nested": {
                      "aggs": {
                        "condi_range": {
                          "filter": {
                            "range": {
                              "nested_path_of_float_field.float_field": {
                                "gte": 0,
                                "lte": 100
                              }
                            }
                          },
                          "aggs": {
                            "percentile_score": {
                              "percentiles": {
                                "field": "nested_path_of_float_field.float_field"
                              }
                            },
                            "statistic_score": {
                              "extended_stats": {
                                "field": "nested_path_of_float_field.float_field"
                              }
                            }
                          }
                        }
                      },
                      "nested": {
                        "path": "nested_path_of_float_field"
                      }
                    }
                  },
                  "reverse_nested": {}
                }
              }
            }
          }
        }
      },
      "nested": {
        "path": "nested_path_of_Foo"
      }
    }
  }
}