Class: SolrCloud::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AliasAdmin, CollectionAdmin, ConfigsetAdmin
Defined in:
lib/solr_cloud/connection.rb,
lib/solr_cloud/connection/version.rb,
lib/solr_cloud/connection/alias_admin.rb,
lib/solr_cloud/connection/configset_admin.rb,
lib/solr_cloud/connection/collection_admin.rb

Overview

The connection object is the basis of all the other stuff. Everything will be created, directly or indirectly, through the connection.

For convenience, it forwards #get, #post, #put, and #delete HTTP verbs to the underlying raw faraday http client.

Defined Under Namespace

Modules: AliasAdmin, CollectionAdmin, ConfigsetAdmin

Constant Summary collapse

VERSION =
"0.6.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AliasAdmin

#alias_map, #alias_names, #aliases, #create_alias, #get_alias, #get_alias!, #has_alias?, #raw_alias_map

Methods included from CollectionAdmin

#collection_names, #collections, #create_collection, #get_collection, #get_collection!, #has_collection?, #only_collection_names, #only_collections

Methods included from ConfigsetAdmin

#configset_names, #configsets, #create_configset, #delete_configset, #get_configset, #has_configset?

Constructor Details

#initialize(url:, user: nil, password: nil, logger: nil, adapter: :httpx) ⇒ Connection

Create a new connection to talk to solr just be the root url (not including the ‘/solr`)

Parameters:

  • url (String)

    URL to the “root” of the solr installation. For a default solr setup, this will

  • user (String) (defaults to: nil)

    username for basic auth, if you’re using it

  • password (String) (defaults to: nil)

    password for basic auth, if you’re using it

  • logger (#info, :off, nil) (defaults to: nil)

    An existing logger to pass in. The symbol “:off” means don’t do logging. If left undefined, will create a standard ruby logger to $stdout

  • adapter (Symbol) (defaults to: :httpx)

    The underlying http library to use within Faraday



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/solr_cloud/connection.rb', line 78

def initialize(url:, user: nil, password: nil, logger: nil, adapter: :httpx)
  @url = url
  @user = user
  @password = password
  @logger = case logger
            when :off, :none
              Logger.new(File::NULL, level: Logger::FATAL)
            when nil
              Logger.new($stderr, level: Logger::WARN)
            else
              logger
            end
  @connection = create_raw_connection(url: url, adapter: adapter, user: user, password: password, logger: @logger)
  bail_if_incompatible!
  @logger.info("Connected to supported solr at #{url}")
end

Instance Attribute Details

#connectionFaraday::Connection (readonly)

Returns the underlying Faraday connection.

Returns:

  • (Faraday::Connection)

    the underlying Faraday connection



45
46
47
# File 'lib/solr_cloud/connection.rb', line 45

def connection
  @connection
end

#logger#info (readonly)

Returns The logger.

Returns:

  • (#info)

    The logger



42
43
44
# File 'lib/solr_cloud/connection.rb', line 42

def logger
  @logger
end

#passwordString (readonly)

Returns Solr password.

Returns:

  • (String)

    Solr password



39
40
41
# File 'lib/solr_cloud/connection.rb', line 39

def password
  @password
end

#urlString (readonly)

Returns String representation of the URL to solr.

Returns:

  • (String)

    String representation of the URL to solr



32
33
34
# File 'lib/solr_cloud/connection.rb', line 32

def url
  @url
end

#userString (readonly) Also known as: username

Returns solr user.

Returns:

  • (String)

    solr user



35
36
37
# File 'lib/solr_cloud/connection.rb', line 35

def user
  @user
end

Class Method Details

.new_from_faraday(faraday_connection) ⇒ Object

Pass in your own faraday connection

Parameters:

  • faraday_connection (Faraday::Connection)

    A pre-build faraday connection object



97
98
99
100
101
102
# File 'lib/solr_cloud/connection.rb', line 97

def self.new_from_faraday(faraday_connection)
  c = allocate
  c.instance_variable_set(:@connection, faraday_connection)
  c.instance_variable_set(:@url, faraday_connection.build_url.to_s)
  c
end

Instance Method Details

#_version_part_int(index) ⇒ Integer

Helper method to get version parts as ints

Returns:

  • (Integer)

    Integerized version of the 0,1,2 portion of the version string



160
161
162
# File 'lib/solr_cloud/connection.rb', line 160

def _version_part_int(index)
  version_string.split(".")[index].to_i
end

#bail_if_incompatible!Object

Check to see if we can actually talk to the solr in question raise [UnsupportedSolr] if the solr version isn’t at least 8 raise [ConnectionFailed] if we can’t connect for some reason



126
127
128
129
130
131
# File 'lib/solr_cloud/connection.rb', line 126

def bail_if_incompatible!
  raise UnsupportedSolr.new("SolrCloud::Connection needs at least solr 8") if major_version < 8
  raise UnsupportedSolr.new("SolrCloud::Connection only works in solr cloud mode") unless cloud?
rescue Faraday::ConnectionFailed
  raise ConnectionFailed.new("Can't connect to #{url}")
end

#cloud?Boolean

Returns whether or not solr is running in cloud mode.

Returns:

  • (Boolean)

    whether or not solr is running in cloud mode



149
150
151
# File 'lib/solr_cloud/connection.rb', line 149

def cloud?
  mode == "solrcloud"
end

#create_raw_connection(url:, adapter: :httpx, user: nil, password: nil, logger: nil) ⇒ Object

Create a Faraday connection object to base the API client off of

See Also:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/solr_cloud/connection.rb', line 106

def create_raw_connection(url:, adapter: :httpx, user: nil, password: nil, logger: nil)
  Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }, url: URI(url)) do |faraday|
    faraday.use Faraday::Response::RaiseError
    faraday.request :url_encoded
    if user
      faraday.request :authorization, :basic, user, password
    end
    faraday.request :json
    faraday.response :json
    if logger
      faraday.response :logger, logger
    end
    faraday.adapter adapter
    faraday.headers["Content-Type"] = "application/json"
  end
end

#deleteObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.delete


63
# File 'lib/solr_cloud/connection.rb', line 63

def_delegator :@connection, :delete

#getObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.get


53
# File 'lib/solr_cloud/connection.rb', line 53

def_delegator :@connection, :get

#inspectObject Also known as: to_s



188
189
190
# File 'lib/solr_cloud/connection.rb', line 188

def inspect
  "<#{self.class} #{@url}>"
end

Check to see if the given string follows solr’s rules for thing Solr only allows ASCII letters and numbers, underscore, and dash, and it can’t start with an underscore.

Parameters:

  • str (String)

    string to check

Returns:

  • (Boolean)


184
185
186
# File 'lib/solr_cloud/connection.rb', line 184

def legal_solr_name?(str)
  !(/[^a-zA-Z_\-.0-9]/.match?(str) or str.start_with?("-"))
end

#major_versionInteger

Returns solr major version.

Returns:

  • (Integer)

    solr major version



165
166
167
# File 'lib/solr_cloud/connection.rb', line 165

def major_version
  _version_part_int(0)
end

#minor_versionInteger

Returns solr minor version.

Returns:

  • (Integer)

    solr minor version



170
171
172
# File 'lib/solr_cloud/connection.rb', line 170

def minor_version
  _version_part_int(1)
end

#modeString

Returns the mode (“solrcloud” or “std”) solr is running in.

Returns:

  • (String)

    the mode (“solrcloud” or “std”) solr is running in



144
145
146
# File 'lib/solr_cloud/connection.rb', line 144

def mode
  system["mode"]
end

#patch_versionInteger

Returns solr patch version.

Returns:

  • (Integer)

    solr patch version



175
176
177
# File 'lib/solr_cloud/connection.rb', line 175

def patch_version
  _version_part_int(2)
end

#postObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.post


58
# File 'lib/solr_cloud/connection.rb', line 58

def_delegator :@connection, :post

#pretty_print(q) ⇒ Object



194
195
196
# File 'lib/solr_cloud/connection.rb', line 194

def pretty_print(q)
  q.text inspect
end

#putObject

Forwarded on to the underlying Faraday connection

See Also:

  • Faraday::Connection.put


68
# File 'lib/solr_cloud/connection.rb', line 68

def_delegator :@connection, :put

#systemHash

Get basic system info from the server

Returns:

  • (Hash)

    The response from the info call

Raises:



136
137
138
139
140
141
# File 'lib/solr_cloud/connection.rb', line 136

def system
  resp = get("/solr/admin/info/system")
  resp.body
rescue Faraday::UnauthorizedError
  raise Unauthorized.new("Server reports failed authorization")
end

#version_stringString

Returns the major.minor.patch string of the solr version.

Returns:

  • (String)

    the major.minor.patch string of the solr version



154
155
156
# File 'lib/solr_cloud/connection.rb', line 154

def version_string
  system["lucene"]["solr-spec-version"]
end