从ElasticSearch索引返回最近的记录 [英] Return the most recent record from ElasticSearch index

查看:71
本文介绍了从ElasticSearch索引返回最近的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ElasticSearch索引返回类似于下面的sql查询的最新记录(前1个);

I would like to return the most recent record (top 1) from ElasticSearch index similar to the sql query below;

SELECT TOP 1 Id, name, title 
FROM MyTable 
ORDER BY Date DESC;

可以这样吗?

推荐答案

你有 _timestamp 在您的文档映射中启用?

Do you have _timestamp enabled in your doc mapping?

{
    "doctype": {
        "_timestamp": {
            "enabled": "true",
            "store": "yes"
        },
        "properties": {
            ...
        }
    }
}

你可以在这里查看你的映射:

You can check your mapping here:

http://localhost:9200/_all/_mapping

如果是这样,我认为这可能是最近得到的:

If so I think this might work to get most recent:

{
  "query": {
    "match_all": {}
  },
  "size": 1,
  "sort": [
    {
      "_timestamp": {
        "order": "desc"
      }
    }
  ]
}

这篇关于从ElasticSearch索引返回最近的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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