Class: SimpleSolrClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_solr_client/client.rb,
lib/simple_solr_client/core.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.



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

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.



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

def base_url
  @base_url
end

#rawclientObject (readonly)

Returns the value of attribute rawclient.



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

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



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

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



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

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:



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

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



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

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:



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

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



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

def major_version
  system.solr_major_version
end

#new_core(corename) ⇒ Object

Create a new, temporary core noinspection RubyWrongHash



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

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



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

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:



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

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???



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

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



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

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

#temp_coreObject



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

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

#temp_core_dir_setup(corename) ⇒ Object

Set up files for a temp core



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

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)



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

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

#unload_temp_coresObject

Unload all cores whose name includes ‘sstemp’



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

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)


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

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



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

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

#versionString

Returns The solr semver version.

Returns:

  • (String)

    The solr semver version



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

def version
  system.solr_semver_version
end