简体   繁体   English

是否可以在JRuby on Rails应用程序中嵌入Solr服务器?

[英]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). 我想在Ruby on Rails应用程序中使用Solr和Sunspot(它将部署在JRuby环境中)。

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. 因此,我不想让最终用户(不一定是具有强大软件部署/开发技能的人)自己安装Solr,我希望webapp能够嵌入Solr服务器。

I thought about using http://wiki.apache.org/solr/EmbeddedSolr , but it won't work well, specially with Sunspot. 我想过使用http://wiki.apache.org/solr/EmbeddedSolr ,但它不会很好用,尤其是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. 我的第一个想法是提取Solr jars和web.xml,将它放在我的Rails应用程序中并将Sunspot指向我的本地应用程序,但我想知道是否有人曾经做过它以及是否有更简单的方法。

Sunspot uses RSolr to communicate with Solr. 太阳黑子使用RSolr与Solr通信。 RSolr uses the standard HTTP interface of Solr. RSolr使用Solr的标准HTTP接口。 So if you want to use embedded Solr you'll have to use SolrJ , or somehow adapt Sunspot to use SolrJ instead of RSolr. 因此,如果你想使用嵌入式Solr,你必须使用SolrJ ,或者以某种方式调整Sunspot以使用SolrJ代替RSolr。

Still, I'd think about it twice before using embedded Solr (see the wiki ). 不过,在使用嵌入式Solr之前我会考虑两次(参见维基 )。 If you use the included Jetty, Solr doesn't need any installation process. 如果使用附带的Jetty,Solr不需要任何安装过程。

I got this working in a rudimentary way with jruby and solrj. 我用jruby和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. solr有很多依赖关系,我没有时间精确计算出所有solr组件都需要哪些jar。 So the following class runs through the default solr and lucene downloads, requiring all jars...not very efficient. 所以下面的类运行默认的solr和lucene下载,需要所有的jar ...效率不高。

Download Lucene 4.1, Solr 4.1, commons-fileupload-1.2.2.jar , and jackson-4.0.6-jar-with-dependencies.jar . 下载Lucene 4.1,Solr 4.1, commons-fileupload-1.2.2.jarjackson-4.0.6-jar-with-dependencies.jar The last one is just for com.google.common.cache.CacheBuilder class. 最后一个仅适用于com.google.common.cache.CacheBuilder类。 I couldn't find it elsewhere. 我在其他地方找不到它。

Put them in a solr-jars directory. 将它们放在solr-jars目录中。 The save the following in a .rb file and run it with jruby. 将以下内容保存在.rb文件中并使用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("*:*")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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