Searching for Dates

184 views
Skip to first unread message

Finding_Zion

unread,
Jan 20, 2009, 1:28:06 PM1/20/09
to Thinking Sphinx
I need to add a range search for dates. The usage guide mentions that
dates are converted to timestamps, however it would be nice to have
some examples for how to search them. This is what I have so far and
it isn't search the way I'd expect.

In the model:
indexes data.pub_date, :as => :pub_date

Phase 1 (Search for a date field)
When searching, I add the following condition.
:conditions => {:pub_date => "8/31/2008".to_time.to_i}

This should be an exact match for 4 records.

Phase 2 (Search for a range and my ultimate goal)
I need to take this and search for a range. According to
http://stackoverflow.com/questions/355937/filtering-sphinx-search-results-by-date-range

I should be able to do a range like:

:conditions => {:pub_date =>
"8/1/2008".to_time.to_i.."9/10/2008".to_time.to_i}

How can I fix this?

Finding_Zion

unread,
Jan 20, 2009, 5:07:54 PM1/20/09
to Thinking Sphinx
I figured out what the problem was with the way I set up the index. I
had to set the type to datetime, perhaps it has to do with my database
table using a date type.

In the model:
has data.pub_date, :type => :datetime, :as => :pub_date

Searching:
def advanced_search
conditions = filter_type
search = params.dup.delete_if {|k,v| !"title author id cb_number
season isbn".include? k}# List items to include
search.each do |key, value|
conditions[key.to_sym] = value if !value.blank?
end

# Search Date range
if !params[:pub_date][:start].blank? && !params[:pub_date]
[:start].blank?
start_date = params[:pub_date][:start].to_i
end_date = params[:pub_date][:end].to_time.advance(:days =>
1).to_i
conditions[:pub_date] = start_date..end_date
end

Project.search :include => :data,
:conditions => conditions,
:page => params[:page] || 1,
:order => "title asc",
:per_page => 10,
:match_mode => :extended
end

On Jan 20, 12:28 pm, Finding_Zion <jeremy.g.peter...@gmail.com> wrote:
> I need to add a range search for dates.  The usage guide mentions that
> dates are converted to timestamps, however it would be nice to have
> some examples for how to search them.  This is what I have so far and
> it isn't search the way I'd expect.
>
> In the model:
>   indexes data.pub_date, :as => :pub_date
>
> Phase 1 (Search for a date field)
> When searching, I add the following condition.
>   :conditions => {:pub_date => "8/31/2008".to_time.to_i}
>
> This should be an exact match for 4 records.
>
> Phase 2 (Search for a range and my ultimate goal)
> I need to take this and search for a range.  According tohttp://stackoverflow.com/questions/355937/filtering-sphinx-search-res...

Pat Allan

unread,
Jan 20, 2009, 11:02:06 PM1/20/09
to thinkin...@googlegroups.com
Hi Jeremy

In the latest versions of Thinking Sphinx, you shouldn't need the
explicit :type => :datetime - and you also shouldn't need .to_i for
the range arguments. If you're using the latest and it's not working,
then we can investigate further - but good to know you've got
something that does the job at the moment.

Cheers

--
Pat

Jeremy Peterson

unread,
Jan 21, 2009, 3:03:25 PM1/21/09
to thinkin...@googlegroups.com
I upgraded ts and it takes care of the .to_i.  However I still need to explicitly call :type => datetime.  I think this is because I am using just a date field.

Sweet,

Jeremy

Pat Allan

unread,
Jan 21, 2009, 9:45:23 PM1/21/09
to thinkin...@googlegroups.com
Hmm, I have a test for date columns... odd.
At least to_i is covered.

Cheers

--
Pat

On 22/01/2009, at 3:03 AM, Jeremy Peterson wrote:

> I upgraded ts and it takes care of the .to_i. However I still need
> to explicitly call :type => datetime. I think this is because I am
> using just a date field.
>
> Sweet,
>
> Jeremy
>
> On Tue, Jan 20, 2009 at 10:02 PM, Pat Allan <pat@freelancing-
Reply all
Reply to author
Forward
0 new messages