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) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
# File 'lib/simple_solr_client/client.rb', line 16

def initialize(url)
  @base_url  = url.chomp('/')
  @client_url = @base_url
  @rawclient = HTTPClient.new
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



53
54
55
56
57
58
59
60
61
# File 'lib/simple_solr_client/client.rb', line 53

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



68
69
70
71
# File 'lib/simple_solr_client/client.rb', line 68

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:



91
92
93
# File 'lib/simple_solr_client/client.rb', line 91

def core(corename)
  SimpleSolrClient::Core.new(@base_url, corename)
end

#coresObject



96
97
98
# File 'lib/simple_solr_client/client.rb', line 96

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:



75
76
77
78
# File 'lib/simple_solr_client/client.rb', line 75

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

#new_core(corename) ⇒ Object

Create a new, temporary core noinspection RubyWrongHash



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/simple_solr_client/client.rb', line 103

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

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

Post an object as JSON and return a Response object

Returns:



82
83
84
85
# File 'lib/simple_solr_client/client.rb', line 82

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 You can pass in :force_top_level=>true for those cases wehn you absolutely have to use the client-level url and not a core level URL



39
40
41
42
43
44
45
46
47
# File 'lib/simple_solr_client/client.rb', line 39

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

#temp_coreObject



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

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

#temp_core_dir_setup(corename) ⇒ Object

Set up files for a temp core



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

def temp_core_dir_setup(corename)
  dest = Dir.mktmpdir("simple_solr_#{corename}")
  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)



30
31
32
# File 'lib/simple_solr_client/client.rb', line 30

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

#unload_temp_coresObject

Unload all cores whose name includes ‘sstemp’



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

def unload_temp_cores
  cores.each do |k|
    core(k).unload if k =~ /sstemp/
  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



24
25
26
# File 'lib/simple_solr_client/client.rb', line 24

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