views:

143

answers:

1

I have a couple specific needs for my search and I'm interested to get people's opinions on what search approach makes the most sense. Based on my explanation below, would you recommend that I use basic sql queries? Or step up to a more advanced search solution, like Sphinx?

I have two models that I want to search in: products and varieties.

product has_many :varieties
variety belongs_to :product

I need my search to recognize the relationship between products and varieties. However, varieties do not have their own existence on the site. So, when a user searches for a variety that's in the system, I need the search to return the corresponding product page on which the variety resides.

For example, let's say that the product is ball and the variety is bouncy. If a user searches for 'bouncy', I want the search to return the ball/show view.

The other tweak involves the results. If there's only one result for a given search, I want to render the product/show page. However, if there are multiple results, I want to render the product/index page, displaying the multiple results. My dataset is a pretty limited universe, so I think it's going to be fairly common that we have only one result.

Those are my requirements. Can I satisfy these requirements with standard sql queries and conditions? Or would you recommend a more advanced search approach?

Thanks!

+4  A: 

Either solution will satisfy your requirement, but you can satisfy it with standard SQL queries only if your dataset is small. In that case, a DB index on the searched queries is important. You could take a look at scoped_search which I've used for small projects and gets the job done.

If you have a big dataset and plain SQL queries slow you down, sphinx (and thinking-sphinx) is the way to go. The only disadvantage of this approach is having to monitor and maintain another daemon, although it is very stable and lightweight. This solution is also very easy to implement, and there's a good community around thinking-sphinx.

Lastly, you may consider your database's full text search capabilities. If you are using PostgreSQL, tsearch is a great solution because it is very fast and built into your database process. There are a couple of Rails plugins for interacting with it: acts-as-tsearch and tsearchable. Try them out and see which one feels better to you.

hgimenez
Thanks. Just implemented scoped_search and it was quick and easy. Any suggestions about how to implement the results logic that I want to achieve? If only one result --> product/show. If multiple --> product/index. I'm thinking I can add some conditional language to the product controller, but not sure exactly how to do it.
MikeH
In your index action on your productions_controller: redirect_to :action => 'show' if @products.length == 1
hgimenez
Thanks. When I tried that, I get this: Couldn't find Product with ID=show. The app tries to send me to the url: /products/show, instead of products/id
MikeH
Makes sense, my bad. Try something like redirect_to product_path( @products.first ) if @products.length == 1. Basically you weren't providing the @product before.
hgimenez
Got it to work with this: redirect_to(@products) if @products.length == 1. I'm having some weird routing problems. I did rake:routes and there is a products/show action. Not sure what's going on. But thanks for the great tips!
MikeH