Class: SolrCloud::Collection
- Inherits:
-
Object
- Object
- SolrCloud::Collection
- Extended by:
- Forwardable
- Defined in:
- lib/solr_cloud/collection.rb
Overview
A Collection provides basic services on the collection -- checking its health, creating or reporting aliases, and deleting itself.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The name of the get_collection.
-
#name ⇒ SolrCloud::Connection
readonly
The underlying SolrCloud connection.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add(docs) ⇒ Object
Add a document or array of documents.
-
#alias? ⇒ Boolean
Is this an alias? Putting this in here breaks all sorts of isolation principles, but being able to call #get_alias? on anything collection-like is convenient.
-
#alias_as(alias_name, force: true) ⇒ SolrCloud::Alias
Create an alias for this collection.
- #alias_as!(alias_name) ⇒ Object
-
#alias_names ⇒ Array<String>
The names of the aliases that point to this collection.
-
#aliased? ⇒ Boolean
Does this collection have at least one alias?.
-
#aliases ⇒ Array<SolrCloud::Alias>
A (possibly empty) list of aliases targeting this collection.
-
#alive? ⇒ Boolean
(also: #ping)
Check to see if the collection is alive.
- #collection ⇒ Object
-
#commit(hard: false) ⇒ Object
Send a commit (soft if unspecified).
-
#configset ⇒ SolrCloud::ConfigSet
What configset was this created with?.
-
#count ⇒ Fixnum
Get a count of how many documents are in the index.
-
#delete ⇒ Object
Forwarded on to the underlying SolrCloud connection.
-
#delete! ⇒ Connection
Delete this collection.
-
#exist? ⇒ Boolean
Does this collection still exist?.
-
#get ⇒ Object
Forwarded on to the underlying SolrCloud connection.
-
#get_alias(alias_name) ⇒ Alias?
Get an alias that points to this collection by name, or nil if not found.
-
#has_alias?(alias_name) ⇒ Boolean
Do we have an alias of the given name? Get a specific alias by name.
-
#healthy? ⇒ Boolean
Reported as healthy?.
-
#info ⇒ Hash
Access to the root info from the api.
-
#initialize(name:, connection:) ⇒ Collection
constructor
In general, users shouldn't use Collection.new; instead, use connection.create_collection(name: "coll_name", configset: "existing_configset_name").
- #inspect ⇒ Object (also: #to_s)
-
#post ⇒ Object
Forwarded on to the underlying SolrCloud connection.
- #pretty_print(q) ⇒ Object
-
#put ⇒ Object
Forwarded on to the underlying SolrCloud connection.
Constructor Details
#initialize(name:, connection:) ⇒ Collection
In general, users shouldn't use Collection.new; instead, use connection.create_collection(name: "coll_name", configset: "existing_configset_name")
45 46 47 48 49 50 |
# File 'lib/solr_cloud/collection.rb', line 45 def initialize(name:, connection:) # raise NoSuchCollectionError.new("No collection #{name}") unless connection.has_collection?(name) @connection = connection.dup @name = name @sp = "/solr/#{name}" end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns The name of the get_collection.
38 39 40 |
# File 'lib/solr_cloud/collection.rb', line 38 def connection @connection end |
#name ⇒ SolrCloud::Connection (readonly)
Returns the underlying SolrCloud connection.
35 36 37 |
# File 'lib/solr_cloud/collection.rb', line 35 def name @name end |
Instance Method Details
#==(other) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/solr_cloud/collection.rb', line 56 def ==(other) case other when SolrCloud::Collection collection.name == other.collection.name else false end end |
#add(docs) ⇒ Object
Gotta check for errors and such!
Add a document or array of documents
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/solr_cloud/collection.rb', line 189 def add(docs) docarray = if docs === Array docs else [docs] end post("solr/#{name}/update/") do |r| r.body = docarray end self end |
#alias? ⇒ Boolean
Is this an alias? Putting this in here breaks all sorts of isolation principles, but being able to call #get_alias? on anything collection-like is convenient
95 96 97 |
# File 'lib/solr_cloud/collection.rb', line 95 def alias? false end |
#alias_as(alias_name, force: true) ⇒ SolrCloud::Alias
Create an alias for this collection. Always forces an overwrite unless you tell it not to
156 157 158 |
# File 'lib/solr_cloud/collection.rb', line 156 def alias_as(alias_name, force: true) connection.create_alias(name: alias_name, collection_name: name, force: true) end |
#alias_as!(alias_name) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/solr_cloud/collection.rb', line 160 def alias_as!(alias_name) if connection.has_alias?(alias_name) raise AliasAlreadyDefinedError.new("Alias #{alias_name} already points to #{connection.get_alias(alias_name).collection.name}") end alias_as(alias_name, force: false) end |
#alias_names ⇒ Array<String>
The names of the aliases that point to this collection
126 127 128 |
# File 'lib/solr_cloud/collection.rb', line 126 def alias_names aliases.map(&:name) end |
#aliased? ⇒ Boolean
Does this collection have at least one alias?
132 133 134 |
# File 'lib/solr_cloud/collection.rb', line 132 def aliased? !aliases.empty? end |
#aliases ⇒ Array<SolrCloud::Alias>
A (possibly empty) list of aliases targeting this collection
120 121 122 |
# File 'lib/solr_cloud/collection.rb', line 120 def aliases connection.alias_map.select { |_alias_name, ac| ac.collection.name == name }.values.map(&:alias) end |
#alive? ⇒ Boolean Also known as: ping
Check to see if the collection is alive
83 84 85 86 87 |
# File 'lib/solr_cloud/collection.rb', line 83 def alive? connection.get("solr/#{name}/admin/ping").body["status"] rescue Faraday::ResourceNotFound false end |
#collection ⇒ Object
52 53 54 |
# File 'lib/solr_cloud/collection.rb', line 52 def collection self end |
#commit(hard: false) ⇒ Object
Send a commit (soft if unspecified)
169 170 171 172 173 174 175 176 |
# File 'lib/solr_cloud/collection.rb', line 169 def commit(hard: false) if hard connection.get("solr/#{name}/update", commit: true) else connection.get("solr/#{name}/update", softCommit: true) end self end |
#configset ⇒ SolrCloud::ConfigSet
What configset was this created with?
180 181 182 |
# File 'lib/solr_cloud/collection.rb', line 180 def configset Configset.new(name: info["configName"], connection: connection) end |
#count ⇒ Fixnum
Get a count of how many documents are in the index
203 204 205 |
# File 'lib/solr_cloud/collection.rb', line 203 def count get("solr/#{name}/select", q: "*:*").body["response"]["numFound"] end |
#delete ⇒ Object
Forwarded on to the underlying SolrCloud connection
27 |
# File 'lib/solr_cloud/collection.rb', line 27 def_delegator :@connection, :delete |
#delete! ⇒ Connection
Delete this collection. Will no-op if the collection somehow doesn't still exist (because it was deleted via a different method, such as through the API)
68 69 70 71 72 73 |
# File 'lib/solr_cloud/collection.rb', line 68 def delete! return connection unless exist? raise CollectionAliasedError.new("Cannot delete collection #{name}; it has alias(s) #{alias_names.join(", ")}") if aliased? connection.get("solr/admin/collections", {action: "DELETE", name: name}) connection end |
#exist? ⇒ Boolean
Does this collection still exist?
77 78 79 |
# File 'lib/solr_cloud/collection.rb', line 77 def exist? connection.only_collection_names.include?(name) end |
#get ⇒ Object
Forwarded on to the underlying SolrCloud connection
17 |
# File 'lib/solr_cloud/collection.rb', line 17 def_delegator :@connection, :get |
#get_alias(alias_name) ⇒ Alias?
Get an alias that points to this collection by name, or nil if not found
147 148 149 150 |
# File 'lib/solr_cloud/collection.rb', line 147 def get_alias(alias_name) return nil unless has_alias?(alias_name) connection.get_alias(alias_name) end |
#has_alias?(alias_name) ⇒ Boolean
Do we have an alias of the given name? Get a specific alias by name
140 141 142 |
# File 'lib/solr_cloud/collection.rb', line 140 def has_alias?(alias_name) alias_names.include?(alias_name) end |
#healthy? ⇒ Boolean
Reported as healthy?
114 115 116 |
# File 'lib/solr_cloud/collection.rb', line 114 def healthy? info["health"] == "GREEN" end |
#info ⇒ Hash
Access to the root info from the api. Mostly for internal use, but not labeled as such 'cause users will almost certainly find a use for it.
Uses the V1 CLUSTERSTATUS action rather than the V2 api/collections/{name}
endpoint because the V2 response envelope changed between Solr 8 and Solr 9/10
(the cluster wrapper key is absent in Solr 10). The V1 CLUSTERSTATUS response
consistently returns cluster.collections.{name} on all supported versions.
108 109 110 |
# File 'lib/solr_cloud/collection.rb', line 108 def info connection.get("solr/admin/collections", action: "CLUSTERSTATUS", collection: name).body["cluster"]["collections"][name] end |
#inspect ⇒ Object Also known as: to_s
207 208 209 210 211 212 213 214 215 |
# File 'lib/solr_cloud/collection.rb', line 207 def inspect anames = alias_names astring = if anames.empty? "" else " (aliased by #{anames.map { |x| "'#{x}'" }.join(", ")})" end "<#{self.class} '#{name}'#{astring}>" end |
#post ⇒ Object
Forwarded on to the underlying SolrCloud connection
22 |
# File 'lib/solr_cloud/collection.rb', line 22 def_delegator :@connection, :post |
#pretty_print(q) ⇒ Object
219 220 221 |
# File 'lib/solr_cloud/collection.rb', line 219 def pretty_print(q) q.text inspect end |
#put ⇒ Object
Forwarded on to the underlying SolrCloud connection
32 |
# File 'lib/solr_cloud/collection.rb', line 32 def_delegator :@connection, :put |