簡體   English   中英

返回elasticsearch中的時間戳字段

[英]Returning the timestamp field in elasticsearch

為什么在能夠通過它過濾查詢時看不到_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"}
    }]
  }

啟用時間戳字段時,它會被索引但默認情況下不會存儲。 因此,雖然您可以按時間戳字段進行搜索和過濾,但您無法使用記錄輕松檢索它。 為了能夠檢索時間戳字段,您需要使用以下映射重新創建索引:

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

這樣,您就可以將時間戳檢索為自紀元以來的毫秒數。

沒有必要存儲時間戳字段,因為它的確切值被保留為一個術語,它也更可能已經存在於RAM中,特別是如果您正在查詢它。 您可以使用script_value通過其術語訪問時間戳:

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

從UNIX紀元開始,結果值以毫秒表示。 ElasticSearch無法為你做到這一點非常淫穢,但嘿,沒有什么是完美的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM