[LRUG] Sphincter Search: Works In Console, Not In App

Andrew Stewart boss at airbladesoftware.com
Fri Feb 29 07:24:37 PST 2008


Happily I've fixed the problem now.  I thought I'd follow up here for  
future reference.


Exhibit A:

On 28 Feb 2008, at 20:11, Andrew Stewart wrote:
> On 28 Feb 2008, at 19:28, David Salgado wrote:
>> OK, I'm sure you've already covered this, but you are looking at  
>> the same environment via console and your app. server, aren't you?
> Both are running in the development environment.

Exhibit B:

> Together these lead me to suspect a broken mapping between Sphinx's  
> index and database table ids.


It turns out that both these suspicions were correct.

The difference between script/console (development mode) and a Rails  
app (development mode) is that while Rails doesn't cache your  
classes, script/console does.

Sphincter mixes a method into ActiveRecord::Base called add_index.   
You declare this in any models you want to be searchable.

When you call add_index, it adds self, i.e. your class, to a  
Sphincter hash whose keys are classes.  That hash holds all the  
indexes that Sphincter knows about, and the total number of entries  
is used in the arithmetic that converts index ids to and from  
database ids.

Hashes test for key equality using #object_id.  If a searchable model  
in your app is reloaded by Rails, the add_index method will add it  
again to Sphincter's hash.  This is because although the second  
version of the class is supposed to be the "same" class, it's  
actually distinct (even if not different) and has a different  
#object_id.

The end result is Sphincter's hash of indexes contains "duplicates"  
and the count is too high, throwing off all the id conversions.

A fix is to make the add_index method check the hash for any classes  
with the same name (rather than #object_id) before adding a new one.   
More details here:

http://rubyforge.org/tracker/index.php? 
func=detail&aid=18483&group_id=1513&atid=5921

Regards,
Andy Stewart

-------
http://airbladesoftware.com






More information about the Chat mailing list