0

I have an indexed field defined as :date that is a method to determine which DateTime object to store.

settings index: { number_of_shards: 1 } do
    mapping do
      ...
      indexes :date,            index: :not_analyzed, type: 'date'
      ...
    end
end

And in my as_indexed_json I have it listed under :methods

def as_indexed_json(options = {})
  self.as_json(
    ...
    methods: [:full_text, :tag_list, :category_list, :date, :month, :year],
    ...
  )
end

It properly indexes the desired value as "date": "2016-05-19T09:43:09-04:00" but when I read the value after searching, the date comes back as class String instead of any of the date classes.

I have fiddled with some of the available format values on that field such as date_time_no_millis, strict_ date_time_no_millis, and "yyyy-MM-dd'T'HH:mm:ssZ", but it appears that that only adjusts the setting and understanding of the value, rather than the reading and class hydration of them.

How can I get this date value to read back as a Rails DateTime as it exists in my DB?

Previously posted as a Github Issue with no response.

4

2 回答 2

0

您是否在问如何在 Rails 中将字符串转换为日期时间值?

> "2016-05-19T09:43:09-04:00".to_datetime => Thu, 19 May 2016 09:43:09 -0400

于 2016-10-21T00:09:05.410 回答
0

String如果我正确理解了这个问题,您只需要DateTime在您的 ruby​​ 应用程序中将日期字段从转换为。

Elasticsearch 将_source存储为 JSON,而 JSON 没有日期数据类型,因此您必须在字符串上调用一个方法,将其转换回日期时间。您可以参考文档以了解日期如何存储在 ES 中。

于 2016-10-21T01:10:08.510 回答