Integration of SOLR/Lucene search with a Rails application - which gems? Hints?
Dear coders, has anyone experience with a RoR integration for SOLR/Lucene? In more detail: I set up a test index with solr/lucene for my rails app. I need to exchange my search functionality with a solr search now.
I want to update the index automatically, not triggered by the application (for now), so no need for adding documents out of the app... I just need to pass the search string over to solr, receive the results and present them in the webapp. What would be the easiest, quickest way to achieve this?
Which gems would you generally use for SOLR/Lucene integration: solr-ruby the acts_as_solr rails plugin seems to be not supported a开发者_运维技巧ny more - would you use it anyway?
As I am a real trial and error learning by doer ;-), a hint for a good integration tutorial and/or some code snippets how to communicate with the solr server could help me a lot.
Kind regards and many thanks! Markus
The best options these days for what you're describing would be Sunspot for its easy to use DSL and ActiveRecord hooks.
Another good option is straight-up RSolr (which Sunspot uses in the background) if you prefer the minimalism of plain Ruby hashes and writing your own ActiveRecord hooks.
Here's the two-minute tutorial for Sunspot:
Gemfile
gem 'sunspot_rails'
app/models/post.rb
class Post < ActiveRecord::Base
searchable do
text :title
text :body
end
end
To index the existing contents of your model from the console (there are also Rake tasks available):
Post.reindex
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def search
@search = Post.search do
keywords params[:q]
end
@posts = @search.results
end
end
app/views/posts/search.html.erb
<h1>Search results</h1>
<%= render @posts %>
We're using SunSpot for quite a few (large dataset) apps without any problems. acts_as_solr has given us a lot of headaches in the past.
精彩评论