class SimpleSolrClient::Client
A Client
talks to the Solr instance; use a SimpleSolrClient::Core
to talk to a particular core.
Attributes
Public Class Methods
# 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
Public Instance Methods
A basic get to the instance (not any specific core) @param [String] path The parts of the URL that comes after the core @param [Hash] args The url arguments @return [Hash] the parsed-out response
# 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 data.
@param [String] path The parts of the URL that comes after the core @param [Hash,Array] object_to_post The data to post as json @return [Hash] the parsed-out response
# 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
Get a client specific to the given core2 @param [String] corename The name of the core (which must already exist!) @return [SimpleSolrClient::Core]
# 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
Get all the cores
# File lib/simple_solr_client/client.rb, line 139 def cores cdata = get('admin/cores', {:force_top_level_url => true}).status.keys end
Get from solr, and return a Response
object of some sort @return [SimpleSolrClient::Response, response_type]
# 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
@return [Integer] the solr major version
# File lib/simple_solr_client/client.rb, line 57 def major_version system.solr_major_version end
Create a new, temporary core
noinspection RubyWrongHash
# 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
# File lib/simple_solr_client/client.rb, line 42 def ping get('admin/ping') end
Post an object as JSON and return a Response
object @return [SimpleSolrClient::Response, response_type]
# 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
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???
# 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
Get info about the solr system itself
# File lib/simple_solr_client/client.rb, line 47 def system @system ||= SimpleSolrClient::System.new(get('admin/info/system')) end
# File lib/simple_solr_client/client.rb, line 161 def temp_core new_core('sstemp_' + SecureRandom.uuid) end
Set up files for a temp core
# 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
Sometimes, you just gotta have a top_level_url
(as opposed to a core-level URL)
# File lib/simple_solr_client/client.rb, line 38 def top_level_url(*args) [@client_url, *args].join('/').chomp('/') end
Unload all cores whose name includes 'sstemp'
# 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
Is the server up (and responding to a ping?) @return [Boolean]
# File lib/simple_solr_client/client.rb, line 63 def up? begin ping.status == 'OK' rescue false end end
Construct a URL for the given arguments that hit the configured solr @return [String] the new url, based on the base_url
and the passed args
# File lib/simple_solr_client/client.rb, line 32 def url(*args) [@base_url, *args].join('/').chomp('/') end
@return [String] The solr semver version
# File lib/simple_solr_client/client.rb, line 52 def version system.solr_semver_version end