Class: RSolr::Connection::Direct

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rsolr/connection/direct.rb

Overview

Connection for JRuby + DirectSolrConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#build_param, #build_url, #bytesize, #escape, #hash_to_query

Constructor Details

#initialize(opts, &block) ⇒ Direct

opts can be an instance of org.apache.solr.servlet.DirectSolrConnection if opts is NOT an instance of org.apache.solr.servlet.DirectSolrConnection then… required: opts is absolute path to solr home (the directory with “data”, “config” etc.) opts must also contain either

:dist_dir => 'absolute path to solr distribution root

or

:jar_paths => ['array of directories containing the solr lib/jars']

OTHER OPTS:

:select_path => 'the/select/handler'
:update_path => 'the/update/handler'


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rsolr/connection/direct.rb', line 25

def initialize(opts, &block)
  if defined?(Java::OrgApacheSolrCore::SolrCore) and opts.is_a?(Java::OrgApacheSolrCore::SolrCore)
    @connection = org.apache.solr.servlet.DirectSolrConnection.new(opts)
  elsif defined?(Java::OrgApacheSolrServlet::DirectSolrConnection) and opts.is_a?(Java::OrgApacheSolrServlet::DirectSolrConnection)
    @connection = opts
  else
    opts[:data_dir] ||= File.join(opts[:home_dir].to_s, 'data')
    if opts[:dist_dir] and ! opts[:jar_paths]
      # add the standard lib and dist directories to the :jar_paths
      opts[:jar_paths] = [File.join(opts[:dist_dir], 'lib'), File.join(opts[:dist_dir], 'dist')]
    end
    @opts = opts
  end
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



12
13
14
# File 'lib/rsolr/connection/direct.rb', line 12

def opts
  @opts
end

Instance Method Details

#closeObject



49
50
51
52
53
54
# File 'lib/rsolr/connection/direct.rb', line 49

def close
  if @connection
    @connection.close
    @connection=nil
  end
end

#connectionObject

loads/imports the java dependencies sets the @connection instance variable if it has not yet been set



42
43
44
45
46
47
# File 'lib/rsolr/connection/direct.rb', line 42

def connection
  @connection ||= (
    require_jars(@opts[:jar_paths]) if @opts[:jar_paths]
    org.apache.solr.servlet.DirectSolrConnection.new(opts[:home_dir], @opts[:data_dir], nil)
  )
end

#request(path, params = {}, data = nil, opts = {}) ⇒ Object

send a request to the connection request ‘/select’, :q=>‘something’ request ‘/update’, :wt=>:xml, ‘</commit>’



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rsolr/connection/direct.rb', line 59

def request(path, params={}, data=nil, opts={})
  data = data.to_xml if data.respond_to?(:to_xml)
  url = build_url(path, params)
  begin
    body = connection.request(url, data)
  rescue
    raise RSolr::RequestError.new($!.message)
  end
  {
    :body=>body,
    :url=>url,
    :path=>path,
    :params=>params,
    :data=>data,
  }
end