如何用一种格式从弹性搜索中获取日期



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

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

这就是您看到的_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"
           ]
        }
     }
  ]

相关内容

  • 没有找到相关文章

最新更新