从ElasticSearch索引返回最新记录

Yas*_*sir 51 elasticsearch

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

SELECT TOP 1 Id, name, title 
FROM MyTable 
ORDER BY Date DESC;
Run Code Online (Sandbox Code Playgroud)

可以这样做吗?

mco*_*lin 60

您是否在文档映射中启用了_timestamp

{
    "doctype": {
        "_timestamp": {
            "enabled": "true",
            "store": "yes"
        },
        "properties": {
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:9200/_all/_mapping
Run Code Online (Sandbox Code Playgroud)

如果是这样,我认为这可能有助于获得最新的:

{
  "query": {
    "match_all": {}
  },
  "size": 1,
  "sort": [
    {
      "_timestamp": {
        "order": "desc"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • `_timestamp` 对我不起作用。但“@timestamp”做到了。 (2认同)

Adr*_*nde 10

出于提供信息的目的,自2.0.0-beta2以来,_timestamp现已弃用.date在映射中使用type.

来自date数据类型doc的简单日期映射JSON :

{
  "mappings": {
     "my_type": {
        "properties": {
          "date": {
          "type": "date" 
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您还可以formatdate以下位置添加字段:

{
  "mappings": {
    "my_type": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Ser*_*eyB 6

由于这个问题最初被提出并得到回答,Elasticsearch 的一些内部工作方式已经发生了变化,特别是在时间戳方面。这是一个完整的示例,展示了如何查询单个最新记录。在 ES 6/7 上测试。

1)告诉Elasticsearch将timestamp字段视为时间戳

curl -XPUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d '{"mappings":{"message":{"properties":{"timestamp":{"type":"date"}}}}}'
Run Code Online (Sandbox Code Playgroud)

2)将一些测试数据放入索引中

curl -XPOST "localhost:9200/my_index/message/1" -H 'Content-Type: application/json' -d '{ "timestamp" : "2019-08-02T03:00:00Z", "message" : "hello world" }'
curl -XPOST "localhost:9200/my_index/message/2" -H 'Content-Type: application/json' -d '{ "timestamp" : "2019-08-02T04:00:00Z", "message" : "bye world" }'
Run Code Online (Sandbox Code Playgroud)

3)查询最新记录

curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d '{"query": {"match_all": {}},"size": 1,"sort": [{"timestamp": {"order": "desc"}}]}'
Run Code Online (Sandbox Code Playgroud)

4)预期结果

{
   "took":0,
   "timed_out":false,
   "_shards":{
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":2,
      "max_score":null,
      "hits":[
         {
            "_index":"my_index",
            "_type":"message",
            "_id":"2",
            "_score":null,
            "_source":{
               "timestamp":"2019-08-02T04:00:00Z",
               "message":"bye world"
            },
            "sort":[
               1564718400000
            ]
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)


dad*_*net 5

您可以使用sort on date字段和size = 1参数.有帮助吗?


Jay*_*kat 5

使用日期获取最后的ID(不带时间戳)

示例网址http:// localhost:9200 / deal / dealsdetails /
方法:POST

查询:

{
  "fields": ["_id"],
  "sort": [{
      "created_date": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "size": 1
}
Run Code Online (Sandbox Code Playgroud)

结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": null,
    "hits": [{
      "_index": "deal",
      "_type": "dealsdetails",
      "_id": "10",
      "_score": 1,
      "sort": [
        1478266145174,
        1
      ]
    }]
  }
}
Run Code Online (Sandbox Code Playgroud)