Search code examples
ruby-on-railsthinking-sphinx

Displaying similar users


Is it possible using Thinking Sphinx for it to output 8 (or other specified) similar users? For example I am on a Males profile page that lives in California. Under the Similar Users section it would show 8 other Males from California. It would be a random 8, no specific order. Just the gender and state location of the current user page needs to be the same.

I understand implementing geo distance with Sphinx, curious to how to show similar results based on user information. Does someone have an example of this?


Solution

  • You could take the geodistance approach as suggested by iMacTia - though I fear that may be overkill.

    I'm presuming both state and gender are fields in your Sphinx index for users. This is in the context of your controller, where you're viewing @user and want similar users.

    User.search(
      :conditions => {:state => @user.state, :gender => @user.gender}
      :without    => {:sphinx_internal_id => @user.id},
      :order      => 'RAND()',
      :per_page   => 8
    )
    

    Breaking down each of those options:

    • Filter by the appropriate state and gender
    • Don't include the current user (the model's primary key is an attribute in Sphinx named sphinx_internal_id).
    • Random order
    • Only eight results