简体   繁体   中英

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:

  • Enter: OMG Happy Cats

    • It searches the whole database looking for anything that has OMG Happy Cats and returns me a an array of model objects that contain that value, that I can then use Active model serializer ( Very important to be able to use this ) on to return you a json object of search results so you can display what ever you want to the user.

      So that json object, if this was a blog, would have a post object, maybe a category object and even a comment object.

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM