弹性搜索获取最新的文档,按多个字段分组 [英] Elasticsearch get the latest documents, grouped by multiple fields

查看:116
本文介绍了弹性搜索获取最新的文档,按多个字段分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询弹性搜索上每种类型的最新文档类似,我在ES中有一组记录。为了这个例子,让我们说这是新闻,每个都有映射:

Similarly to Query the latest document of each type on Elasticsearch, I have a set of records in ES. For the sake of the example, lets say it's news as well, each with mapping:

"news": {
    "properties": {
        "source": { "type": "string", "index": "not_analyzed" },
        "headline": { "type": "object" },
        "timestamp": { "type": "date", "format": "date_hour_minute_second_millis" },
        "user": { "type": "string", "index": "not_analyzed" }
        "newspaper": { "type": "string", "index": "not_analyzed"}
    }
}

我可以通过以下方式获取每个用户的最新新闻文章:

I am able to get the latest 'news article' per user with:

"size": 0,
"aggs": {
    "sources" : {
        "terms" : {
            "field" : "user"
        },
        "aggs": {
            "latest": {
              "top_hits": {
                "size": 1,
                "sort": {
                  "timestamp": "desc"
                }
              }
            }
        }
    }
}

实现的是每个用户每个报纸获得最后一篇文章

However what I am trying to achieve is to get the last article per user, per newspaper and I cannot get it quite right.

例如


  • 约翰,纽约时报,Title1

  • 约翰,BBC ,Title2

  • 简·纽约时报,Title3

  • 等。

  • John, NY Times, Title1
  • John, BBC, Title2
  • Jane, NY Times, Title3
  • etc.

推荐答案

您可以为条款子集合>

"size": 0,
"aggs": {
    "sources" : {
        "terms" : {
            "field" : "user"
        },
        "aggs": {
            "newspaper": {
               "terms": {
                  "field": "newspaper"
               },
               "aggs": {
                  "latest": {
                     "top_hits": {
                       "size": 1,
                       "sort": {
                          "timestamp": "desc"
                       }
                     }
                  }
               }
            }
        }
    }
}

这篇关于弹性搜索获取最新的文档,按多个字段分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆