返回弹性搜索中的时间戳记字段 [英] Returning the timestamp field in elasticsearch

查看:88
本文介绍了返回弹性搜索中的时间戳记字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我可以看不到_timestamp字段,同时可以过滤查询?

Why can I not see the _timestamp field while being able to filter a query by it?

以下查询返回正确的文档,但不返回时间戳本身。如何返回时间戳?

The following query return the correct documents, but not the timestamp itself. How can I return the timestamp?

{
  "fields": [
    "_timestamp",
    "_source"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "_timestamp": {
            "from": "2013-01-01"
          }
        }
      }
    }
  }
}

映射是:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            }
        }
    }
}

示例输出:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test1",
      "_type" : "doctype1",
      "_id" : "HjfryYQEQL6RkEX3VOiBHQ",
      "_score" : 1.0, "_source" : {"cards": "5"}
    }, {
      "_index" : "test1",
      "_type" : "doctype1",
      "_id" : "sDyHcT1BTMatjmUS0NSoEg",
      "_score" : 1.0, "_source" : {"cards": "2"}
    }]
  }


推荐答案

当时间戳记字段启用时,它被编入索引,但默认情况下不存储。所以,当您可以按时间戳字段搜索和过滤时,您无法轻松地用记录检索它。为了能够检索时间戳记字段,您需要使用以下映射重新创建索引:

When timestamp field is enabled, it's indexed but not stored by default. So, while you can search and filter by the timestamp field, you cannot easily retrieve it with your records. In order to be able to retrieve the timestamp field you need to recreate your index with the following mapping:

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

通过这种方式,您将能够以时间戳记的形式检索时间戳。

This way you will be able to retrieve timestamp as the number of milliseconds since the epoch.

这篇关于返回弹性搜索中的时间戳记字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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