elasticsearch没有附加默认值

时间:2015-02-09 22:57:53

标签: elasticsearch

我已经尝试过很多迭代,但它根本没有工作。我希望自动将_timestamp或@timestamp附加到每个文档。 似乎不能让它发挥作用。

尽管文件正在被正确摄取

curl -XPOST 'http://X.X.X.X:9200/associations' -d '{
"settings" : {
    "number_of_shards" : 1
},
"mappings" : {
    "_default_":{
        "_timestamp" : {
            "enabled" : true,
            "store" : true,
             "path" : "post_date"
        }
    }
  }
}'

我也试过直接在我的索引

中设置它
curl -XPUT 'http://X.X.X.X:9200/associations' -d  '{
    "mappings": {
       "_timestamp" :
       {
           "enabled":true,
           "store": "yes",
           "path" : "post_date",
           "type": "date",
           "format" : "yyyy-MM-dd HH:mm:ss"

       }
     }
}'

1 个答案:

答案 0 :(得分:0)

您拥有的第一个代码块似乎对我有用。如果我用它来定义索引:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0
   },
   "mappings": {
      "_default_": {
         "_timestamp": {
            "enabled": true,
            "store": true,
            "path": "post_date"
         }
      }
   }
}

然后将一些文档添加到新类型中:

PUT /test_index/doc/1
{
    "post_date": "2015-1-25"
}

PUT /test_index/doc/2
{
    "post_date": "2015-1-15"
}

我现在可以看到新映射中的时间戳:

GET /test_index/_mapping
...
{
   "test_index": {
      "mappings": {
         "_default_": {
            "_timestamp": {
               "enabled": true,
               "store": true,
               "path": "post_date"
            },
            "properties": {}
         },
         "doc": {
            "_timestamp": {
               "enabled": true,
               "store": true,
               "path": "post_date"
            },
            "properties": {
               "post_date": {
                  "type": "date",
                  "format": "dateOptionalTime"
               }
            }
         }
      }
   }
}

我可以搜索该类型,并在fields中询问"_timestamp",我会在结果中找回时间戳:

POST /test_index/doc/_search
{
    "fields": [
       "_timestamp",
       "post_date"
    ]
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "fields": {
               "post_date": [
                  "2015-1-25"
               ],
               "_timestamp": 1422144000000
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1,
            "fields": {
               "post_date": [
                  "2015-1-15"
               ],
               "_timestamp": 1421280000000
            }
         }
      ]
   }
}

以下是我使用的代码:

http://sense.qbox.io/gist/1ab1ecb73d3e87cffe0052ce1706e7985d197fad

顺便说一句,我正在运行Elasticsearch 1.3.4版。