I am building a rails app where users can post, must like a form. I want users to be able to search through the posts with a text field, and then all records will be returned where the post content is similar to the user query. For example, a user enters:
'albert einstein atomic bomb'
Then, I want a to run a query where every word is checked, along with the query itself. Something like:
query_result = Hash.new
query_result << Post.where('content ILIKE ?', "%albert%")
query_result << Post.where('content ILIKE ?', "%einstein%")
query_result << Post.where('content ILIKE ?', "%atomic%")
query_result << Post.where('content ILIKE ?', "%bomb%")
query_result << Post.where('content ILIKE ?', "%albert einstein atomic bomb%")
This will not work of course, but I hope you get the idea. Any and all input would be appreciated.
You can use sunspot gem for stuff like this. Once it’s setup you can do searches like so
and more. See the info in the link.