Search code examples
ruby-on-railsthinking-sphinx

comparison Operators in thinking Sphinix


I have a model with attributes start_date and end_date. I have search form where user will put the date and I should get a data from the model if date is in between start_date and end_date.

how should I create a query with thinking sphinx.


Solution

  • You will need to do something like the following:

    • Add both start_date and end_date as attributes (not fields) to your model's Sphinx index.
    • Translate form params into a date or time value
    • Use range filters to limit search queries.

    I've opted for very large windows of time, but essentially this ensures the given date is equal to or larger than the start date and less than or equal to the end date.

    beginning, ending = Time.utc(1970), Time.utc(2030)
    Model.search :with => {
      :start_date => beginning..date_from_params,
      :end_date   => date_from_params..ending
    }