lucene-ruby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Hatcher <e...@ehatchersolutions.com>
Subject DirectSolrConnection / JRuby
Date Thu, 09 Oct 2008 01:08:12 GMT
DirectSolrConnection does work out pretty nicely:


require 'java'
require 'erb'

solr_dist_root = "/Users/erik/apache-solr-1.3.0"
solr_home = "/Users/erik/apache-solr-1.3.0/example/solr"

def require_jars(dir)
   jar_pattern = File.join(dir,"**", "*.jar")
   jar_files = Dir.glob(jar_pattern)

   jar_files.each {|jar_file| require jar_file}
end

require_jars(File.join(solr_dist_root, "lib"))
require_jars(File.join(solr_dist_root, "dist"))

import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer
import org.apache.solr.core.CoreContainer
import org.apache.solr.core.CoreDescriptor
import org.apache.solr.common.params.ModifiableSolrParams
import org.apache.solr.common.params.MapSolrParams
import org.apache.solr.servlet.DirectSolrConnection

container = CoreContainer.new
descriptor = CoreDescriptor.new(container, "core1", solr_home)
core = container.create(descriptor)
container.register("core1", core, false)

class Request
   def initialize(params)
     @params = params
   end

   def handler
     'select'
   end

   def content_type
     'application/x-www-form-urlencoded; charset=utf-8'
   end

   def to_s
     http_params = []
     @params.each do |key,value|
       if value.respond_to? :each
         value.each { |v| http_params <<  
"#{key}=#{ERB::Util::url_encode(v)}" unless v.nil?}
       else
         http_params << "#{key}=#{ERB::Util::url_encode(value)}"  
unless value.nil?
       end
     end

     http_params.join("&")
   end
end

solr = DirectSolrConnection.new(solr_home,File.join(solr_home,"solr",  
"data"),nil)
request = Request.new(:qt => 'standard', :q => '*:*', 'facet.field' =>  
['cat'], :wt=>'ruby')
response = solr.request("/#{request.handler}?#{request.to_s}",nil)
puts response
solr.close
core.close

The response is the raw String response, suitable for eval'ing if  
wt=ruby.

So, for embedded SolrJRuby work, this will be the most transparent way  
to go I think.

The question I have is whether we should concern ourselves with  
SolrJ's CommonsHttpSolrServer?  I suspect if you're in a JRuby  
environment that SolrJ's API will be faster performance-wise than  
eval'ing Ruby responses.  Testing warranted for sure.

	Erik


Mime
View raw message