Topic: Questions on How to Implement a Search

Hello all!

I've been lurking these forums for a bit, now, but am still unsure of exactly where this post should go. If it's in the wrong spot, please let me know and I'll try and rectify the problem.

So here's my question, with a bit of background first.

To put it in the most simple terms, I have a Users table, that will have many joins with other tables which will create the properties of that user (ie. address information, skills information, etc).

What I would like to do is create a textbox where a user of the website can search for other users by typing in various properties. These properties, of course, would be located in all of the join tables.

So say a user types in "Dallas Ruby on Rails C++". I would like to be able to take those properties and make intelligent searches on the database, to return intelligent results.

I thought of two solutions that I could do... one is not elegant, and one doesn't really make my database elegant.

1) I would take every phrase typed into the search and search through all of the properties of all users. It would require going through each table to find out if the keyword was found in those tables, and if so, get the users from that search... This is just not simple enough for me, though, and I feel there's a better solution.

2) When a user types in all of their information, I could save the information in the database as normal (So that I could still display it to someone in an intelligable manner), but also take all of those properties and create tags for the user based on those properties. Then, I'd simply make the User have acts_as_taggable, and use the User.find_tagged_with('SEARCH TERMS') ... That would make searching a bit more intuitive and simple to code, but I now have data in two places in my database... Which makes me itch a bit, heh.

I'm leaning towards solution two, but I'm wondering if there's a better way to do this. Any help would be greatly appreciated! If more information is needed, I'll gladly provide.

Thanks in advance!

Last edited by SilentSeraph (2006-11-28 13:42:31)

Re: Questions on How to Implement a Search

Solution #2 is the closes to the indexing method used by many small search engines.  The idea being that you have this store of data (like your users and their information) but then you have this other, search-optimized index of that data.

I like the second option because, while it could take some time to build the index, it allows you to respond to searches faster - and more accurately if you do a good job indexing the data.

For the actual implementation, I suggest looking into some after_save callbacks on your user model and it's connected models.  Have this after_save call some sort of indexing method and you'll be all set!

Re: Questions on How to Implement a Search

Wow! Thanks for the speedy response!

I was planning on doing something along the lines of this... This is rough at best and off the top of my head, but it gives you an idea of where I'm trying to go...

Also, I was planning on following this guide:
Acts As Taggable Plugin Howto

#Save the new data...
newuser =
newuser.FirstName = params[:firstname]
newuser.LastName = params[:lastname]
# More properties to save...

#Now tag the user with all the properties...

newuser.tag_with('#{newuser.Firstname} #{newuser.LastName} #{newuser.ETC}')

Would this be sort of what you're talking about? I would probably place the newuser.tag_with line in the after_save hook, to keep things simple...

How would you suggest writing some sort of indexing in the after_save method? Not quite sure what you're talking about...