Elasticsearch文档指出The top_hits aggregation returns regular search hits, because of this many per hit features can be supported
最重要的是,该列表包含Named filters and queries
但尝试添加任何过滤器或查询会引发SearchParseException: Unknown key for a START_OBJECT
用例:我有包含嵌套注释列表的项目
项{id} - >评论{date,rating}
我希望在上周获得每个项目的评分最高。
{
"query": {
"match_all": {}
},
"aggs": {
"items": {
"terms": {
"field": "id",
"size": 10
},
"aggs": {
"comment": {
"nested": {
"path": "comments"
},
"aggs": {
"top_comment": {
"top_hits": {
"size": 1,
//need filter here to select only comments of last week
"sort": {
"comments.rating": {
"order": "desc"
}
}
}
}
}
}
}
}
}
}
文档错误,或者有没有办法添加过滤器?
答案 0 :(得分:-1)
您确定已将它们映射为Nested
吗?我只是尝试对我的数据执行此类查询,但它确实运行良好。
如果是这样,你可以简单地在嵌套聚合之后添加一个过滤器聚合(希望我没有弄乱大括号):
POST data/_search
{
"size": 0,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "comments",
"query": {
"range": {
"comments.date": {
"gte": "now-1w",
"lte": "now"
}
}
}
}
}
}
},
"aggs": {
"items": {
"terms": {
"field": "id",
"size": 10
},
"aggs": {
"nested": {
"nested": {
"path": "comments"
},
"aggs": {
"filterComments": {
"filter": {
"range": {
"comments.date": {
"gte": "now-1w",
"lte": "now"
}
}
},
"aggs": {
"topComments": {
"top_hits": {
"size": 1,
"sort": {
"comments.rating": "desc"
}
}
}
}
}
}
}
}
}
}
}
P.S。始终包含嵌套对象的FULL路径。
所以这个查询将: