Sara Trice
Just a programmin', bellydancin', cake bakin' kinda girl

  • RSS-Feed posts
  • RSS-Feed comments

  • gallery
  • livejournal
  • résumé
  • website portfolio

Twitter

Tweets by @saratrice

Pages

  • gallery
  • livejournal
  • résumé
  • website portfolio

Fun Stuff

  • My Amazon Wish List

Webcomics

  • Dark Legacy Comics
  • Erfworld
  • Questionable Content

New Posts

  • DIY syrups!
  • Women’s Olympic Foil 2012
  • Paperclip, Amazon S3 Storage, the Amazon AWS-SDK gem, and Error: “No such file or directory”
  • Indexing users that belong to groups with ancestry and thinking sphinx
  • Avoiding Ruby hash conditionals in Ruby on Rails

Latest Comments

  • 22.05 | David in ImageMagick: "convert: color profile operates on a…
  • 23.04 | Chintan in Using javan/whenever on Engine Yard Cloud
  • 10.04 | saratrice in Using javan/whenever on Engine Yard Cloud
  • 10.04 | Nic Pillinger in Using javan/whenever on Engine Yard Cloud
  • 11.12 | Christian in jsTree: adding Expand All and Collapse All buttons…
March 2012
S M T W T F S
« Feb   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031
21
Mar

Indexing users that belong to groups with ancestry and thinking sphinx

So you’ve created a table of groups that are in a hierarchy with the gem ancestry, and you’ve created a table of users, and you’ve joined users to groups with a join table.

And now you want to use Thinking Sphinx, which is awesome for searching. So you think, hey, wouldn’t it be great if I could do a search for all users in a group’s subtree? Except that Thinking Sphinx cares not for your puny ancestry methods, and :subtree_ids doesn’t work in an index.

Fear not! I’ve done the heavy lifting for you.

in user.rb:
define_index do
  indexes last_name
  has groups(:id), :as => :direct_group_ids
  has "CONCAT_WS('/',groups.id,groups.ancestry)", :as => :group_ids, :type => :multi
end

The first attribute is so that the join is made from users to groups. The second actually creates the multi-value attribute that you can search on. So from there you can do:

User.search(:with_all => {:group_ids => [1]})

That will give you all the users that belong to the subtree, including the root group (in this case, the group with the id of “1″).

That being said, if you only want the users from sub-groups of the group you’re searching on (i.e. you never want users that are directly attached to the group you’re searching on), you can instead do this:

in user.rb:
define_index do
  indexes last_name
  has groups(:ancestry), :as => :group_ids, :type => :multi
end

So if you have group 1 which has group 2 and group 3 as children, the first example will give you all the users attached to all 3 groups; the second example will only give you the users attached to groups 2 and 3.

One last gotcha: if you’re running the search in console, remember to add “:per_page => 100″ or however many entries you want back, or else by default you only get 20. Don’t want you to headdesk when you can’t figure out why it’s returning 20 users when it’s supposed to be returning 75.

Happy indexing!

by saratrice in tech
no comment

Meta

  • Log in
© 2008 - 2013
Design & CSS by
Freizeitler

Blogroll

    Dark Legacy Comics Yay MMORPGs!
    Erfworld Rob Balder writes this
    My Amazon Wish List Buy me somethin’!
    Questionable Content Am I more like Faye or Dora?