假设我有一个非常简单的索引。 Blog post
和blog categories
。一个博客属于一个或更多类别。
我想为每个类别找到最后3个帖子。我怎样才能做到这一点 ?我在这里阅读了“Field collapsing”https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html但是这个例子是指一个标量字段,我是一个集合。
文件可以是:
{ "title" : "My post",
"categories" : [{ "tech" => "Technology", "professional" => "Professional"]
},
{ "title" : "My secondo post",
"categories" : [{ "professional" => "Professional"]
},
答案 0 :(得分:1)
您只能聚合值而不是键,因此第一步是简化categories
字段。
如果您可以简单地将categories
索引为字符串列表(例如,仅限类别slug,而不是slug => slug_title对),那么您可以使用Terms Aggregation向您展示所有字符串结果集中显示的类别,然后是嵌套的Top Hits Aggregation,以显示按日期排序的前三个帖子。
我相信this example on the Top Hits Aggregation页面正是您想要的(如果您可以将类别索引为字符串列表)。