Searching for Dates

瀏覽次數:184 次
跳到第一則未讀訊息

Finding_Zion

未讀,
2009年1月20日 下午1:28:062009/1/20
收件者: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

未讀,
2009年1月20日 下午5:07:542009/1/20
收件者: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

未讀,
2009年1月20日 晚上11:02:062009/1/20
收件者: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

未讀,
2009年1月21日 下午3:03:252009/1/21
收件者: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

未讀,
2009年1月21日 晚上9:45:232009/1/21
收件者: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-
回覆所有人
回覆作者
轉寄
0 則新訊息