【发布时间】:2016-10-07 21:47:56
【问题描述】:
我想根据script_metric 聚合的结果对我的搜索结果进行排序。但我总是得到错误的回应。
我搜索了一下,发现这是弹性搜索实现的一个限制。不可能基于 script_metric 聚合字段进行排序。
另外,我必须对结果进行分页,因为可能会有很多结果。
所以我无法检索所有结果,然后在代码中对结果进行排序。
我想知道这种情况是否有任何好的选择。 这是我在下面的查询
{
"query": {
"range": {
"creationTimestamp": {
"gte": 1475613000,
"lte": 1475699400
}
}
},
"size": 0,
"aggs": {
"messages_count": {
"terms": {
"field": "sourceId",
"order": {
"totalViews": "desc"
},
"size": 10
},
"aggs": {
"totalViews": {
"scripted_metric": {
"init_script": "_agg['maximum'] = []",
"map_script": "max = _source.histories[_source.histories.values.size()-1].views; _agg.maximum.add(max);",
"combine_script": "sum = 0; for (m in _agg.maximum) { sum += m }; return sum;",
"reduce_script": "sum = 0; for (a in _aggs) { sum += a }; return sum;"
}
}
}
}
}
}
这里是弹性搜索对我的查询的响应:
{
"error": {
"root_cause": [
{
"type": "aggregation_execution_exception",
"reason": "Invalid terms aggregation order path [totalViews]. 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."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "datacollection",
"node": "PS0_HLzxRk-jO_C-_x8ivw",
"reason": {
"type": "aggregation_execution_exception",
"reason": "Invalid terms aggregation order path [totalViews]. 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."
}
}
]
},
"status": 500
}
【问题讨论】:
标签: sorting elasticsearch