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}->评论{日期,评分}
我想在上周获得每个项目的最高评价。
{
"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"
}
}
}
}
}
}
}
}
}
}
那么文档是否错误,或者有什么方法可以添加过滤器?
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-metrics-top-hits-aggregation.html
最佳答案
您确定已将它们映射为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"
}
}
}
}
}
}
}
}
}
}
}
附言始终为嵌套对象包括FULL路径。
因此,此查询将:
关于elasticsearch - Elasticsearch中的过滤器/查询支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319337/