user3379926
user3379926

Reputation: 3945

Search Gems for Rails

I was browsing reddit for the answer to this and came across this conversation which lists out a bunch of search gems for rails, which is cool. But what I wanted was something where I could:

Everything I have seen is very specific to one controller, one model. Which is nice an all but I am more of a "search for what you want, we will return you what you want, maybe grow smarter like this gem, searchkick which also has the ability to offer spelling suggestion.

I am building this with an API, so it would be limited to everything that belongs to a blog object (as to make it not so huge of a search), so it would search things like posts, tags, categories, comments and pages looking for your term, return a json object (as described) and boom done.

Any ideas?

Upvotes: 1

Views: 300

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

You'll be best considering the underlying technology for this

--

Third Party

As far as I know (I'm not super experienced in this area), the best way to search an entire Rails database is to use a third party system to "index" the various elements of data you require, allowing you to search them as required.

Some examples of this include:

Essentially, having one of these "third party" search systems gives you the ability to index the various records you want in a separate database, which you can then search with your application.

--

Notes

There are several advantages to handling "search" with a third party stack.

Firstly, it takes the load off your main web server - which means it'll be more reliable & able to handle more traffic.

Secondly, it will ensure you're able to search all the data of your application, instead of tying into a particular model / data set

Thirdly, because many of these third party solutions index the content you're looking for, it will free up your database connectivity for your actual application, making it more efficient & scaleable

Upvotes: 2

makhan
makhan

Reputation: 4009

For PostgreSQL you should be able to use pg_search. I've never used it myself but going by the documentation on GitHub, it should allow you to do:

documents = PgSearch.multisearch('OMG Happy Cats').to_a
objects = documents.map(&:searchable)
groups = objects.group_by{|o| o.class.name.pluralize.downcase}
json = Hash[groups.map{|k,v| [k,ActiveModel::ArraySerializer.new(v).as_json]}].as_json
puts json.to_json

Upvotes: 1

Related Questions