Date range facets with Sunspot in Ruby on Rails

霸气de小男生 提交于 2019-12-03 08:26:38
outoftime

You should set up your fields as trie time fields for efficient range queries:

class Event
  searchable do
    time :start_time, :trie => true
  end
end

Then you can use query facets to facet based on ranges:

Event.search do
  facet :start_time do
    bod = Time.zone.now.beginning_of_day
    row :today do
      with :start_time, bod..(bod + 1)
    end
    row :tomorrow do
      with :start_time, (bod + 1)..(bod + 2)
    end
    # etc.
  end
end

The key insight here is that you can construct facets using arbitrary scopes. Note that the facet name :start_time is just used to reference the facet in the search results, and the row labels :today and :tomorrow are similarly just used on the client side to identify the row counts corresponding to those queries; they have no meaning from Solr's standpoint, so you can call them whatever you want (using whatever data type you want -- they don't have to be symbols).

More information on query facets here: http://sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method

Patrick Espake

You can do in this way:

with(:registration_date).between(params[:start_date].to_date..params[:end_date].to_date)

Look here: http://sunspot.github.io/docs/Sunspot.html#search-class_method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!