【问题标题】:How get date from elasticsearch with one format?如何以一种格式从弹性搜索中获取日期?
【发布时间】:2015-07-01 22:18:46
【问题描述】:

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

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

【问题讨论】:

  • 您是否尝试在映射的消息属性中显式设置格式,例如format: 'dateOptionalTime'

标签: date datetime elasticsearch


【解决方案1】:

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

【讨论】:

  • 即使文档不是日期格式,它们是如何被编入索引的?
  • @VamsiKrishna 查看 ES 返回的文档列表,您会看到 ES 实际索引的内容。
  • 好的。我的主要问题是,当给定值不是日期格式时,为什么 ES 甚至允许 PUT 请求成功。我说的是OP提供的映射。不是您提供的脚本映射。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多