I've been a huge fan of Swiftype for a while now and have been using it since its beta days. Recently, however, it seems that the availability of its API has decreased causing the Rails gem to throw exceptions. The API will return various HTTP 500 status codes throughout the day. I have been getting multiple emails every day from Rollbar notifying me that the Swiftype gem was throwing exceptions. I can only assume that the folks over at Swiftype are deploying broken code at the most inopportune times of the day. It's gotten so bad, in fact, that I've had to add backup searching capabilities to one of my websites. While searching using a simple SQL LIKE query won't be as good as the results Swiftype gives, I can at least ensure that it's going to be up as long as my website is up. One can only hope that Swiftype will mature and become much more reliable in the future.
For those interested, I'm just catching the exceptions thrown by the Swiftype gem and then searching manually.
client = Swiftype::Easy.new
@results = client.search(ENV['SWIFTYPE_ENGINE_SLUG'], params[:q], {:per_page => '10', :page => params[:page] || 1})
@book_results = @results['book']
if (@book_results.empty? == false)
@books = Array.new
# Build an array of AR models for the view.
@book_results.each do |ar|
@books << Book.find(ar.external_id)
end
end
rescue Exception
# Swiftype API failed. Manually search on a book's title and its author's display name.
query = params[:q].downcase
@books = Book.joins("LEFT JOIN users ON books.user_id = users.id").where("lower(users.displayname) LIKE ? OR lower(books.name) LIKE ?", "%#{query}%", "%#{query}%")
end