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.

coneybeare
  • 33,113
  • 21
  • 131
  • 183

2 Answers2

0

Are you asking how to convert the string to a datetime value in Rails?

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

moveson
  • 5,103
  • 1
  • 15
  • 32
0

If I understand the question correctly, You would have to convert the date field from String to DateTime in your ruby application only.

Elasticsearch stores the _source as JSON and JSON does not have a date datatype so you would have to call a method on the string which converts it back to datetime. You could refer to documentation to see how dates are stored in ES.

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • All other objects are converted into their proper rails objects. String, Hashes etc... My Rails app knows how to map the values to the index with elasticsearch-rails, it is supposed to know how to map back as well, yet it doesn't – coneybeare Oct 21 '16 at 01:50