Elasticsearch映射文档的时间以秒为单位

时间:2015-06-25 09:36:17

标签: elasticsearch

据我所知,es只支持ms中的纪元。我的数据源是couchbase,其中的json文件有一个insertEpoch,存储在几秒钟内。我一直在努力让我的映射文档为我做几秒转换。

这是我的地图文档:

NameValueCollection fakeHeaders = new NameValueCollection();
fakeHeaders.Add("AcceptType", "ABC");
fakeHeaders.Add("AcceptType", "QWE");

Mock<HttpRequestBase> requestBaseMock = new Mock<HttpRequestBase>();
requestBaseMock.Setup(r => r.HttpMethod).Returns("POST");
requestBaseMock.Setup(r => r.Headers).Returns(fakeHeaders);

转变没有发生。

新的地图文档:

{
    "template" : "cb*",
    "order" : 10,
    "mappings" : {
        "couchbaseCheckpoint" : {
            "_source" : {
                "includes" : ["doc.*"]
            },
            "dynamic_templates": [
                {
                    "store_no_index": {
                        "match": "*",
                        "mapping": {
                            "store" : "no",
                            "index" : "no",
                            "include_in_all" : false
                        }
                    }
                }
            ]
        },
        "couchbaseDoc" : {
            "_source" : {
                "includes" : ["meta.*","doc.*"]
            },
            "properties" : {
                "meta" : {
                    "type" : "object",
                    "include_in_all" : false
                },
                "doc" : {
                    "type" : "nested",
                    "include_in_all" : false,
                    "transform": {
                        "script": "ctx._source['insertEpoch'] = ctx._source['insertEpoch'] * 1000",
                        "params": {},
                        "lang": "groovy"
                    }
                }
            }
        }
    }
}

我甚至没有看到我应该看到的_timestamp字段!

这是一个Kibana screenie:

kibana screenshot

由于

1 个答案:

答案 0 :(得分:1)

根据https://github.com/elastic/elasticsearch/pull/10420,这适用于1.6。 即使在内部,日期本身将以毫秒为单位,您可以将其索引为秒,将其检索为秒,这就像您将其编入索引一样。

我已经尝试过一个简单的测试,看看这个是否有效:

PUT /test_dates
{
  "mappings": {
    "test": {
      "properties": {
        "time_stamp": {
          "type": "date",
          "format": "date_time",
          "numeric_resolution": "seconds"
        }
      }
    }
  }
}

测试数据:

POST /test_dates/test/1
{
  "time_stamp": "9231200"
}

检索它:

   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_dates",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "time_stamp": "9231200"
            }
         }
      ]
   }

要证明它有效,请运行此聚合:

GET /test_dates/test/_search?search_type=count
{
  "aggs": {
    "NAME": {
      "date_histogram": {
        "field": "time_stamp",
        "interval": "second",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

返回

   "aggregations": {
      "NAME": {
         "buckets": [
            {
               "key_as_string": "1970-04-17",
               "key": 9231200000,
               "doc_count": 1
            }
         ]
      }
   }

此外,您的模板有点不对劲。它应该是:

"couchbaseDocument": {
    "_timestamp": {
        "enabled": true,
        "store": true
    },
    "properties": {
        "meta": {
            "type": "object",
            "include_in_all": false
        },
        "doc": {
            "type": "object",
            "include_in_all": false,
            "properties": {
                "updateEpoch": {
                    "type": "date",
                    "format": "date_time",
                    "numeric_resolution": "seconds"
                }
            }
        }
    }
}