The use case is this:
I’d like to let my user search from a single text box, then on the search results page organize the results by class, essentially.
So for example, say I have the following models configured for Thinking Sphinx: Post, Comment and User. (In my situation i have about 10 models but for clarity on StackOverflow I’m pretending there are only 3)
When i do a search similar to: ThinkingSphinx.search 'search term', :classes => [Post, Comment, User] I’m not sure the best way to iterate through the results and build out the sections of my page.
My first inclination is to do something like:
- Execute the search
- Iterate over the returned result set and do a result.is_a?(ClassType)
- Based on the ClassType, add the item to 1 of 3 arrays —
@match_posts,@matching_comments, or@matching_users - Pass those 3 instance variables down to my view
Is there a better or more efficient way to do this?
Thank you!
I think it comes down to what’s useful for people using your website. Does it make sense to have the same query run across all models? Then
ThinkingSphinx.searchis probably best, especially from a performance perspective.That said, do you want to group search results by their respective classes? Then some sorting is necessary. Or are you separating each class’s results, like a GitHub search? Then having separate collections may be worthwhile, like what you’ve already thought of.
At a most basic level, you could just return everything sorted by relevance instead of class, and then just render slightly different output depending on each result. A case statement may help with this – best to keep as much of the logic in helpers, and/or possibly partials?