如何从一个格式的elasticsearch获取日期?

时间:2015-07-01 13:18:01

标签: date datetime elasticsearch

我创建了一个像这样的索引:

PUT twitter

PUT twitter/_mapping/myType
{
    "myType" : {
        "properties" : {
            "message" : {"type" : "date",
            "date_detection": true,
            "store" : true }
        }
    }
}

然后我提出了几个文件:

POST twitter/myType
{
    "message":123456
}

我有这个文件和其他message值:“123456”, - 123456,“2014-01-01”,“ - 123456”(注意字符串和数字差异在这里)。只有值为“12 @ 3454”的文档无法放入。

现在我执行:

GET twitter/myType/_search?pretty=true&q=*:*

结果是:

{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 1,
      "hits": [
         {
            "_index": "twitter",
            "_type": "myType",
            "_id": "AU5JvFsHvhUOO_5MdfCv",
            "_score": 1,
            "_source": {
               "message": -123456
            }
         },
         {
            "_index": "twitter",
            "_type": "myType",
            "_id": "AU5Ju6aOvhUOO_5MdfCs",
            "_score": 1,
            "_source": {
               "message": "123456"
            }
         },
         {
            "_index": "twitter",
            "_type": "myType",
            "_id": "AU5Ju0KOvhUOO_5MdfCq",
            "_score": 1,
            "_source": {
               "message": "2014-01-01"
            }
         },
         {
            "_index": "twitter",
            "_type": "myType",
            "_id": "AU5JvDiGvhUOO_5MdfCu",
            "_score": 1,
            "_source": {
               "message": "-123456"
            }
         }
      ]
   }
}

为什么我在日期字段中获取这些值而不是字符串值 - ISODateTimeFormat.dateOptionalTimeParser?有没有办法用一种格式(例如字符串或毫秒)获取所有日期?

Elasticsearch版本是1.4.3

1 个答案:

答案 0 :(得分:2)

那是你看到的_source,意思是你索引的确切JSON,没有格式化,没有。 如果你想看看ES索引的实际内容(意味着以毫秒为单位的日期),你可以使用fielddata_fields

GET /twitter/myType/_search
{
  "query": {
    "match_all": {}
  },
  "fielddata_fields": [
    "message"
  ]
}

你问题的答案是实际上并不是开箱即用的。您需要使用script_fields

GET /twitter/myType/_search
{
  "query": {
    "match_all": {}
  },
  "fielddata_fields": [
    "message"
  ],
  "_source": "*", 
  "script_fields": {
    "my_script": {
      "script": "new Date(doc[\"message\"].value)"
    }
  }
}

此外,您的映射是错误的:date_detection应该放在不在字段中的类型中:

PUT twitter
{
  "mappings": {
    "myType": {
      "date_detection": true,
      "properties": {
        "message": {
          "type": "date",
          "store": true
        }
      }
    }
  }
}

从下面的输出中你会看到ES如何处理你放在那里的那些数字:

     {
        "_index": "twitter",
        "_type": "myType",
        "_id": "AU5J93Q-I7tQJ10g6jk5",
        "_score": 1,
        "_source": {
           "message": "123456"
        },
        "fields": {
           "message": [
              3833727840000000
           ],
           "my_script": [
              "123456-01-01T00:00:00.000Z"
           ]
        }
     },
     {
        "_index": "twitter",
        "_type": "myType",
        "_id": "AU5J93Q-I7tQJ10g6jk4",
        "_score": 1,
        "_source": {
           "message": 123456
        },
        "fields": {
           "message": [
              123456
           ],
           "my_script": [
              "1970-01-01T00:02:03.456Z"
           ]
        }
     },
     {
        "_index": "twitter",
        "_type": "myType",
        "_id": "AU5J93Q-I7tQJ10g6jk8",
        "_score": 1,
        "_source": {
           "message": "-123456"
        },
        "fields": {
           "message": [
              -3958062278400000
           ],
           "my_script": [
              "-123456-01-01T00:00:00.000Z"
           ]
        }
     },
     {
        "_index": "twitter",
        "_type": "myType",
        "_id": "AU5J93Q-I7tQJ10g6jk7",
        "_score": 1,
        "_source": {
           "message": "2014-01-01"
        },
        "fields": {
           "message": [
              1388534400000
           ],
           "my_script": [
              "2014-01-01T00:00:00.000Z"
           ]
        }
     },
     {
        "_index": "twitter",
        "_type": "myType",
        "_id": "AU5J93Q-I7tQJ10g6jk6",
        "_score": 1,
        "_source": {
           "message": -123456
        },
        "fields": {
           "message": [
              -123456
           ],
           "my_script": [
              "1969-12-31T23:57:56.544Z"
           ]
        }
     }
  ]