Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active November 25, 2016 16:11
Show Gist options
  • Save elvisgiv/1c65ca0f5beed2b5bf30f3404ec92a32 to your computer and use it in GitHub Desktop.
Save elvisgiv/1c65ca0f5beed2b5bf30f3404ec92a32 to your computer and use it in GitHub Desktop.

Elasticsearch range by date

Для того, чтобы модель сортировалась в пределах определенного временного отрезка, мы должны передать в ES строку такого вида "2015-01-01T12:10:30Z"

example

module ElasticsearchSearchable
  extend ActiveSupport::Concern
  
  included do
    ...
    settings index: { number_of_shards: 1 } do
      mappings dynamic: 'true' do
        ...
        indexes :created_at,     :index    => :not_analyzed, :type => 'date'
        ...
      end
    end
    
    def self.search(f, t)
      __elasticsearch__.search(
          {
              min_score: 0.5,
              query: {
                  filtered: {
                      filter: {
                          bool: {
                              must: [{
                                  range:
                                   {
                                      created_at: {
                                         gte: "2016-11-25T15:10:43Z",
                                         lte: "2016-11-25T15:39:43Z"
                                      },
                                   }
                              }],
                          },
                      },
                  },
              },
           },
       )
    end
  end
end

https://www.elastic.co/guide/en/elasticsearch/reference/2.1/date.html

https://www.elastic.co/guide/en/elasticsearch/guide/current/_ranges.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment