SOLR / Lucene Search Integration with Rails - What are the Pearls? Advice?

Dear coders, does anyone have any experience integrating with RoR for SOLR / Lucene? In more detail: I installed a test index with solr / lucene for my rails application. I need to exchange search functions using solr search.

I want to update the index automatically, and not be launched by the application (at the moment), so there is no need to add documents from the application ... I just need to pass the search string to solr, get the results and submit them to webapp. What will be the easiest and fastest way to achieve this?

What gems do you usually use to integrate SOLR / Lucene: the solr-ruby plugin actions_as_solr rails does not seem to be supported anymore - do you use it anyway?

Since I am a true error and error research with doer ;-), a tip for a good integration tutorial and / or some code snippets on how to communicate with the solr server can help me a lot.

Best regards and many thanks! Marcus

+3
source share
2 answers

The best options these days for what you are describing will be Sunspot for its easy-to-use DSL and ActiveRecord hooks.

Another good option is directly RSolr (which uses Sunspot in the background) if you prefer the minimalism of simple ruby ​​hashes and writing your own ActiveRecord hooks.

Here is a two-minute tutorial for Sunspot:

Gemfile

gem 'sunspot_rails'

application / models / post.rb

class Post < ActiveRecord::Base
  searchable do
    text :title
    text :body
  end
end

( Rake):

Post.reindex

//posts_controller.rb

class PostsController < ApplicationController
  def search
    @search = Post.search do
      keywords params[:q]
    end
    @posts = @search.results
  end
end

///search.html.erb

<h1>Search results</h1>
<%= render @posts %>
+9

SunSpot ( ) - . act_as_solr .

+2

Source: https://habr.com/ru/post/1787460/


All Articles