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 ⇒ Object
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
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/solr_cloud/collection.rb', line 182 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
149 150 151 |
# File 'lib/solr_cloud/collection.rb', line 149 def alias_as(alias_name, force: true) connection.create_alias(name: alias_name, collection_name: name, force: true) end |
#alias_as!(alias_name) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/solr_cloud/collection.rb', line 153 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
119 120 121 |
# File 'lib/solr_cloud/collection.rb', line 119 def alias_names aliases.map(&:name) end |
#aliased? ⇒ Boolean
Does this collection have at least one alias?
125 126 127 |
# File 'lib/solr_cloud/collection.rb', line 125 def aliased? !aliases.empty? end |
#aliases ⇒ Array<SolrCloud::Alias>
A (possibly empty) list of aliases targeting this collection
113 114 115 |
# File 'lib/solr_cloud/collection.rb', line 113 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)
162 163 164 165 166 167 168 169 |
# File 'lib/solr_cloud/collection.rb', line 162 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?
173 174 175 |
# File 'lib/solr_cloud/collection.rb', line 173 def configset Configset.new(name: info["configName"], connection: connection) end |
#count ⇒ Fixnum
Get a count of how many documents are in the index
196 197 198 |
# File 'lib/solr_cloud/collection.rb', line 196 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
140 141 142 143 |
# File 'lib/solr_cloud/collection.rb', line 140 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
133 134 135 |
# File 'lib/solr_cloud/collection.rb', line 133 def has_alias?(alias_name) alias_names.include?(alias_name) end |
#healthy? ⇒ Boolean
Reported as healthy?
107 108 109 |
# File 'lib/solr_cloud/collection.rb', line 107 def healthy? info["health"] == "GREEN" end |
#info ⇒ Object
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.
101 102 103 |
# File 'lib/solr_cloud/collection.rb', line 101 def info connection.get("api/collections/#{name}").body["cluster"]["collections"][name] end |
#inspect ⇒ Object Also known as: to_s
200 201 202 203 204 205 206 207 208 |
# File 'lib/solr_cloud/collection.rb', line 200 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
212 213 214 |
# File 'lib/solr_cloud/collection.rb', line 212 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 |