简体   繁体   中英

Is it possible do embed a Solr server inside a JRuby on Rails app?

I want to use Solr and Sunspot in a Ruby on Rails app (which will be deployed in a JRuby environment).

But the app will be distributed to the end users, and I want to make the install process as easy as possible. So I don't want to make the end user (which will not be necessarily a guy which strong software deployment/development skills) install Solr by himself, I want the webapp to have a Solr server embedded.

I thought about using http://wiki.apache.org/solr/EmbeddedSolr , but it won't work well, specially with Sunspot.

My first thought is extracting Solr jars and web.xml, putting it in my Rails app and pointing Sunspot to my local app, but I want to know if anyone has ever done it and if there is an easier way of doing it.

Sunspot uses RSolr to communicate with Solr. RSolr uses the standard HTTP interface of Solr. So if you want to use embedded Solr you'll have to use SolrJ , or somehow adapt Sunspot to use SolrJ instead of RSolr.

Still, I'd think about it twice before using embedded Solr (see the wiki ). If you use the included Jetty, Solr doesn't need any installation process.

I got this working in a rudimentary way with jruby and solrj. There are TONS of dependencies for solr and I haven't had the time to work out exactly which jars are required for all solr components. So the following class runs through the default solr and lucene downloads, requiring all jars...not very efficient.

Download Lucene 4.1, Solr 4.1, commons-fileupload-1.2.2.jar , and jackson-4.0.6-jar-with-dependencies.jar . The last one is just for com.google.common.cache.CacheBuilder class. I couldn't find it elsewhere.

Put them in a solr-jars directory. The save the following in a .rb file and run it with jruby.

require 'java'

module Solr

    class SolrServer

        include_package 'org.apache.solr.core'
        include_package 'org.apache.solr.client.solrj'
        include_package 'org.apache.solr.client.solrj.embedded'
        include_package 'org.apache.lucene'

        def initialize(jarpath, solr_home, core)
            Dir["#{jarpath}/**/*.jar"].each { |f| puts f; require f;}
            java.lang.System.setProperty('solr.solr.home',solr_home)
            initializer = CoreContainer::Initializer.new
            coreContainer = initializer.initialize__method()
            @server = EmbeddedSolrServer.new(coreContainer,core)
        end

        def query(q)
            solr_query = SolrQuery.new
            solr_query.setQuery q
            puts @server.query(solr_query)
        end

    end

end

solr = Solr::SolrServer.new('solr-jars','solr-jars/solr-4.1.0/example/solr','collection1')
solr.query("*:*")

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