Array of nil results returned by Thinking Sphinx 5

Posted by Ben Reubenstein Thu, 31 Dec 2009 16:02:00 GMT

Sphinx combined with Thinking Sphinx is a powerful full text search solution for Rails. After using it on several projects, when it started returning an array of nil results, I was perplexed to say the least. After adjusting the model I found the issue to occur when I set an alternate primary key in the model using set_primary_key. When that is set, Sphinx works returning the proper IDs, the SQL executes properly, but the search method returns [nil, nil,....].

According to the Sphinx documentation:

ALL DOCUMENT IDS MUST BE UNIQUE UNSIGNED NON-ZERO INTEGER NUMBERS (32-BIT OR 64-BIT, DEPENDING ON BUILD TIME SETTINGS).

To fix, set_sphinx_primary_key must also be set. In this particular case the table still had the standard id. Do the following in the model:

  set_primary_key :other_primary_key
  set_sphinx_primary_key :id