-1

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.

1 Answers1

2

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
}
pat
  • 16,116
  • 5
  • 40
  • 46
  • thanks pat .this helped me. is there anyway to add sql query to the ThinkingSphinx::Search result. – Ranjeet Lal Bahadur Maurya Dec 10 '14 at 10:53
  • You can send through some options for the resulting SQL query - `:include`, `:joins`, `:select`, `:order`, as noted in the documentation: http://pat.github.io/thinking-sphinx/searching.html#advanced - but that doesn't have any option for adding to the `WHERE` clause (which wouldn't be recommended anyway - it would screw up the pagination logic for Sphinx). – pat Dec 10 '14 at 22:48