Thinking Sphinx with a date range

Posted by Leddo on Stack Overflow See other posts from Stack Overflow or by Leddo
Published on 2010-06-01T18:05:53Z Indexed on 2010/06/01 19:23 UTC
Read the original article Hit count: 357

Hi, I am implementing a full text search API for my rails apps, and so far have been having great success with Thinking Sphinx.

I now want to implement a date range search, and keep getting the "bad value for range" error.

Here is a snippet of the controller code, and i'm a bit stuck on what to do next.

    @search_options = { :page => params[:page], :per_page => params[:per_page]||50 }

    unless params[:since].blank?
        # make sure date is in specified format - YYYY-MM-DD
        d = nil
        begin
            d = DateTime.strptime(params[:since], '%Y-%m-%d')
        rescue
            raise ArgumentError, "Value for since parameter is not a valid date - please use format YYYY-MM-DD"
        end
        @search_options.merge!(:with => {:post_date => d..Time.now.utc})
    end
    logger.info @search_options
    @posts = Post.search(params[:q], @search_options)

When I have a look at the log, I am seeing this bit which seems to imply the date hasn't been converted into the same time format as the Time.now.utc.

withpost_date2010-05-25T00:00:00+00:00..Tue Jun 01 17:45:13 UTC 2010

Any ideas? Basically I am trying to have the API request pass in a "since" date to see all posts after a certain date. I am specifying that the date should be in the YYYY-MM-DD format.

Thanks for your help. Chris

EDIT: I just changed the date parameters merge statement to this

@search_options.merge!(:with => {:post_date => d.to_date..DateTime.now})

and now I get this error

undefined method `to_i' for Tue, 25 May 2010:Date

So obviously there is something still not setup right...

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about thinking-sphinx