I am available for freelance work! Click here to email me.

ThinkingSphinx and Nil Results

November 9th, 2009

Have you ever gotten a result like the following when using ThinkingSphinx (by the awesome Pat Allen):

NoMethodError (undefined method `constantize' for nil:NilClass):

This error occurs when your index isn’t quite up to date and ThinkingSphinx returns results that have since been deleted from your database. To fix the problem add :retry_stale => true to your searches, like this:

Article.search('chunky bacon', :retry_stale => true)

There is more information about :retry_stale in the source code:

# If you pass :retry_stale => true to a single-model search, missing records will
# cause Thinking Sphinx to retry the query but excluding those records. Since search
# is paginated, the new search could potentially include missing records as well, so by
# default Thinking Sphinx will retry three times. Pass :retry_stale => 5 to retry five
# times, and so on. If there are still missing ids on the last retry, they are
# shown as nils.

I hope this saves someone a few minutes in the future :)

(Possibly) Related Posts

Recommend Me

If you found this post or anything else on this site of any use, then please take the time to recommend me on Working with Rails.

You can follow any responses to this entry through the RSS 2.0 feed. Trackback from your own site.

  • joshkalderimis
    Hi Jim,

    This was a recent issue with an app we recently deployed, but was fixed once Pat made a small enhancement to the way destroys work for TS.

    Previously, when using suspended_delta, TS would not remove destroyed objects from the delta index but would instead try to do it after the suspended_delta block call. This did not work because the suspended_delta call did not keep track of the documents to remove, and although a delta index would fix this, if using delayed_job this could possibly be a while away.

    Pat has since changed the behavior of destroys to always remove the document from the delta index, since then I have not had this issue.

    I never knew of retry_stale, thanks for the tip, it may come in handy if the problem pops up it ugly head again.

    Thanks for the post
  • I wish, you have had posted this yesterday.

blog comments powered by Disqus
Purify - Issue Tracker

Jim Neath is a 26 year old Freelance Ruby on Rails developer from Manchester, UK.

Recommend Me

Categories