Returning the timestamp field in elasticsearch

前端 未结 2 1698
一生所求
一生所求 2020-12-31 07:42

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

相关标签:
2条回答
  • 2020-12-31 08:16

    It is not necessary to store the timestamp field, since its exact value is preserved as a term, which is also more likely to already be present in RAM, especially if you are querying on it. You can access the timestamp via its term using a script_value:

    {
        "query": {
            ...
        },
        "script_fields": {
            "timestamp": {
                "script": "_doc['_timestamp'].value"
            }
        }
    }
    

    The resulting value is expressed in miliseconds since UNIX epoch. It's quite obscene that ElasticSearch can't do this for you, but hey, nothing's perfect.

    0 讨论(0)
  • 2020-12-31 08:19

    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.

    0 讨论(0)
提交回复
热议问题