如何在Elasticsearch术语聚合中按多个值排序

时间:2015-04-12 09:10:13

标签: elasticsearch

如何按多个值订购ES术语聚合?

目前我这样做:

aggs : {
    aggName : {
        terms : {
            field : "foo",
            order : { "subAgg.avg" : "desc" }
        }
    },
    aggs : {
        subAgg : {
            stats : {
                field : "bar"
            }
        }
    }
}

API说你可以这样做:

order : [ { "subAgg.avg" : "desc" }, { "subAgg.count" : "desc" } ]

但这不起作用,ES抛出错误:

Unknown key for a START_ARRAY in [aggName]: [order].

我在其他帖子中发现了类似的内容:

order : { "subAgg.avg" : "desc", "subAgg.count" : "desc" }

没有错误,但没有正确排序。

我的问题是,如何正确排序多个值? 我安装了ES 1.4.4。

THX

EDITED: 映射

{
  "mappings" : {
    "mymapping" : {
      "properties" : {
        "foo" : {
          "type" : "short"
        }
      }
    }
  }
}

查询:

{
    query : { 
        match_all : {} 
    },
    aggs : {
        aggName : {
            terms : {
                field : "foo",
                order : [ { "subAgg.avg" : "desc" }, { "subAgg.count" : "desc" } ]
            },
            aggs : {
                subAgg : {
                    stats : {
                        field : "foo"
                    }
                }
            }
        }
    }
}    

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

"order" : [ { "rock>playback_stats.avg" : "desc" }, { "_count" : "desc" } ]

发件人:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

{
"aggs" : {
    "countries" : {
        "terms" : {
            "field" : "artist.country",
            "order" : [ { "rock>playback_stats.avg" : "desc" }, { "_count" : "desc" } ]
        },
        "aggs" : {
            "rock" : {
                "filter" : { "term" : { "genre" : "rock" }},
                "aggs" : {
                    "playback_stats" : { "stats" : { "field" : "play_count" }}
                }
            }
        }
    }
}