Class: SimpleSolrClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_solr_client/client.rb

Overview

A Client talks to the Solr instance; use a SimpleSolrClient::Core to talk to a particular core.

Direct Known Subclasses

Core

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_or_port) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple_solr_client/client.rb', line 17

def initialize(url_or_port)
  url = if url_or_port.is_a?(Integer)
          "http://localhost:#{url_or_port}/solr"
        else
          url_or_port
        end

  @base_url   = url.chomp('/')
  @client_url = @base_url
  @rawclient  = HTTPClient.new

  # raise "Can't connect to Solr at #{url}" unless self.up?
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



15
16
17
# File 'lib/simple_solr_client/client.rb', line 15

def base_url
  @base_url
end

#rawclientObject (readonly)

Returns the value of attribute rawclient.



15
16
17
# File 'lib/simple_solr_client/client.rb', line 15

def rawclient
  @rawclient
end

Instance Method Details

#_get(path, args = {}) ⇒ Hash

A basic get to the instance (not any specific core)

Parameters:

  • path (String)

    The parts of the URL that comes after the core

  • args (Hash) (defaults to: {})

    The url arguments

Returns:

  • (Hash)

    the parsed-out response



95
96
97
98
99
100
101
102
103
# File 'lib/simple_solr_client/client.rb', line 95

def _get(path, args = {})
  path.sub! /\A\//, ''
  args['wt'] = 'json'
  res        = JSON.parse(raw_get_content(path, args))
  if res['error']
    raise RuntimeError.new, res['error']
  end
  res
end

#_post_json(path, object_to_post) ⇒ Hash

post JSON data.

Parameters:

  • path (String)

    The parts of the URL that comes after the core

  • object_to_post (Hash, Array)

    The data to post as json

Returns:

  • (Hash)

    the parsed-out response



110
111
112
113
# File 'lib/simple_solr_client/client.rb', line 110

def _post_json(path, object_to_post)
  resp = @rawclient.post(url(path), JSON.dump(object_to_post), {'Content-type' => 'application/json'})
  JSON.parse(resp.content)
end

#core(corename) ⇒ SimpleSolrClient::Core

Get a client specific to the given core2

Parameters:

  • corename (String)

    The name of the core (which must already exist!)

Returns:



133
134
135
136
# File 'lib/simple_solr_client/client.rb', line 133

def core(corename)
  raise "Core #{corename} not found" unless cores.include? corename.to_s
  SimpleSolrClient::Core.new(@base_url, corename.to_s)
end

#coresObject

Get all the cores



140
141
142
# File 'lib/simple_solr_client/client.rb', line 140

def cores
  cdata = get('admin/cores', {:force_top_level_url => true}).status.keys
end

#get(path, args = {}, response_type = nil) ⇒ SimpleSolrClient::Response, response_type

Get from solr, and return a Response object of some sort

Returns:



117
118
119
120
# File 'lib/simple_solr_client/client.rb', line 117

def get(path, args = {}, response_type = nil)
  response_type = SimpleSolrClient::Response::GenericResponse if response_type.nil?
  response_type.new(_get(path, args))
end

#major_versionInteger

Returns the solr major version.

Returns:

  • (Integer)

    the solr major version



58
59
60
# File 'lib/simple_solr_client/client.rb', line 58

def major_version
  system.solr_major_version
end

#new_core(corename) ⇒ Object

Create a new, temporary core noinspection RubyWrongHash



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/simple_solr_client/client.rb', line 147

def new_core(corename)
  dir = temp_core_dir_setup(corename)

  args = {
    :wt          => 'json',
    :action      => 'CREATE',
    :name        => corename,
    :instanceDir => dir
  }

  get('admin/cores', args)
  core(corename)

end

#pingObject



43
44
45
# File 'lib/simple_solr_client/client.rb', line 43

def ping
  get('admin/ping')
end

#post_json(path, object_to_post, response_type = nil) ⇒ SimpleSolrClient::Response, response_type

Post an object as JSON and return a Response object

Returns:



124
125
126
127
# File 'lib/simple_solr_client/client.rb', line 124

def post_json(path, object_to_post, response_type = nil)
  response_type = SimpleSolrClient::Response::GenericResponse if response_type.nil?
  response_type.new(_post_json(path, object_to_post))
end

#raw_get_content(path, args = {}) ⇒ Object

Call a ‘get’ on the underlying http client and return the content Will use whatever the URL is for the current context (“client” or “core”), although you can pass in :force_top_level=>true for those cases when you absolutely have to use the client-level url and not a core level URL

Error handling? What error handling???



81
82
83
84
85
86
87
88
89
# File 'lib/simple_solr_client/client.rb', line 81

def raw_get_content(path, args = {})
  if args.delete(:force_top_level_url)
    u = top_level_url(path)
  else
    u = url(path)
  end
  res = @rawclient.get(u, args)
  res.content
end

#systemObject

Get info about the solr system itself



48
49
50
# File 'lib/simple_solr_client/client.rb', line 48

def system
  @system ||= SimpleSolrClient::System.new(get('admin/info/system'))
end

#temp_coreObject



162
163
164
# File 'lib/simple_solr_client/client.rb', line 162

def temp_core
  new_core('sstemp_' + SecureRandom.uuid)
end

#temp_core_dir_setup(corename) ⇒ Object

Set up files for a temp core



167
168
169
170
171
172
# File 'lib/simple_solr_client/client.rb', line 167

def temp_core_dir_setup(corename)
  dest = Dir.mktmpdir("simple_solr_#{corename}_#{SecureRandom.uuid}")
  src  = SAMPLE_CORE_DIR
  FileUtils.cp_r File.join(src, '.'), dest
  dest
end

#top_level_url(*args) ⇒ Object

Sometimes, you just gotta have a top_level_url (as opposed to a core-level URL)



39
40
41
# File 'lib/simple_solr_client/client.rb', line 39

def top_level_url(*args)
  [@client_url, *args].join('/').chomp('/')
end

#unload_temp_coresObject

Unload all cores whose name includes ‘sstemp’



175
176
177
178
179
# File 'lib/simple_solr_client/client.rb', line 175

def unload_temp_cores
  cores.each do |k|
    core(k).unload if k =~ /sstemp/
  end
end

#up?Boolean

Is the server up (and responding to a ping?)

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/simple_solr_client/client.rb', line 64

def up?
  begin
    ping.status == 'OK'
  rescue
    false
  end
end

#url(*args) ⇒ String

Construct a URL for the given arguments that hit the configured solr

Returns:

  • (String)

    the new url, based on the base_url and the passed args



33
34
35
# File 'lib/simple_solr_client/client.rb', line 33

def url(*args)
  [@base_url, *args].join('/').chomp('/')
end

#versionString

Returns The solr semver version.

Returns:

  • (String)

    The solr semver version



53
54
55
# File 'lib/simple_solr_client/client.rb', line 53

def version
  system.solr_semver_version
end