Date range facets with Sunspot in Ruby on Rails

前端 未结 2 814
南笙
南笙 2021-02-10 23:03

I am using Sunspot to search for events (parties, concerts, ...) in a Ruby on Rails 3 application.

I have managed to set up free text s

相关标签:
2条回答
  • 2021-02-10 23:39

    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

    0 讨论(0)
  • 2021-02-10 23:53

    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

    0 讨论(0)
提交回复
热议问题