Elasticsearch _timestamp

时间:2012-12-06 13:05:57

标签: elasticsearch elasticsearch-mapping

我尝试在索引上定义_timestamp属性。 首先,我创建了索引

curl -XPUT 'http://elasticsearch:9200/ppe/'

来自服务器的响应:{"ok":true,"acknowledged":true}

然后我尝试使用_timestamp

定义映射
curl -Xput 'http://elasticsearch:9200/ppe/log/_mapping' -d '{
  "log": {
    "properties": {
      "_ttl": {
        "enabled": true
      },
      "_timestamp": {
        "enabled": true,
        "store": "yes"
      },
      "message": {
        "type": "string",
        "store": "yes"
      },
      "appid": {
        "type": "string",
        "store": "yes"
      },
      "level": {
        "type": "integer",
        "store": "yes"
      },
      "logdate": {
        "type": "date",
        "format": "date_time_no_millis",
        "store": "yes"
      }
    }
  }
}'

我收到服务器的答案

{
  "error": "MapperParsingException[No type specified for property [_timestamp]]",
  "status": 400
}

我的地图有什么问题?

2 个答案:

答案 0 :(得分:16)

_ttl_timestamp等特殊字段必须与properties对象在同一级别定义:

curl -Xput 'http://elasticsearch:9200/ppe/log/_mapping' -d '{
    "log": {
        "_ttl": {
            "enabled": true
        },
        "_timestamp": {
            "enabled": true,
            "store": "yes"
        },
        "properties": {
            "message": {
                "type": "string",
                "store": "yes"
            },
            "appid": {
                "type": "string",
                "store": "yes"
            },
            "level": {
                "type": "integer",
                "store": "yes"
            },
            "logdate": {
                "type": "date",
                "format": "date_time_no_millis",
                "store": "yes"
            }
        }
    }
}
'

答案 1 :(得分:3)

请注意,虽然_timestamp在顶级定义,但会在fields内返回:

curl 'http://localhost:9200/myindex/mytype/AUqL0PW7YDMmKSIKO1bk?pretty=true&fields=_timestamp'
{
  "_index" : "myindex",
  "_type" : "mytype",
  "_id" : "AUqL0PW7YDMmKSIKO1bk",
  "_version" : 1,
  "found" : true,
  "fields" : {
    "_timestamp" : 1419684935099
  }
}

请注意,_timestampfields=_timestamp必须明确请求fields=_timestamp,_source

请注意,仅当此字段标记为_timestamp时,才能返回'store': true。但是有一种方法可以在按_timestamp排序时访问此值,如下所示:

curl 'http://localhost:9200/myindex/mytype/_search?pretty=true' -d ' 
   { "sort": [ "_timestamp" ], "size": 1}
 '

给出结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [ {
       "_index" : "myindex",
       "_type" : "mytype",
       "_id" : "AUqL0PDXYDMmKSIKO1bj",
       "_score" : null,
       "sort" : [ 1419684933847 ]
     } ]
  }
}

现在sort[0]是第一个(也是唯一一个)排序值的值:_timestamp。当以这种方式使用时,_timestamp不必标记为"store": true