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
-
#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_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
Check to see if the collection is alive.
-
#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”)
46 47 48 49 50 51 |
# File 'lib/solr_cloud/collection.rb', line 46 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.
39 40 41 |
# File 'lib/solr_cloud/collection.rb', line 39 def connection @connection end |
#name ⇒ SolrCloud::Connection (readonly)
Returns the underlying SolrCloud connection.
36 37 38 |
# File 'lib/solr_cloud/collection.rb', line 36 def name @name end |
Instance Method Details
#add(docs) ⇒ Object
Gotta check for errors and such!
Add a document or array of documents
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/solr_cloud/collection.rb', line 161 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
81 82 83 |
# File 'lib/solr_cloud/collection.rb', line 81 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
135 136 137 |
# File 'lib/solr_cloud/collection.rb', line 135 def alias_as(alias_name, force: true) connection.create_alias(name: alias_name, collection_name: name, force: true) end |
#alias_names ⇒ Array<String>
The names of the aliases that point to this collection
105 106 107 |
# File 'lib/solr_cloud/collection.rb', line 105 def alias_names aliases.map(&:name) end |
#aliased? ⇒ Boolean
Does this collection have at least one alias?
111 112 113 |
# File 'lib/solr_cloud/collection.rb', line 111 def aliased? !aliases.empty? end |
#aliases ⇒ Array<SolrCloud::Alias>
A (possibly empty) list of aliases targeting this collection
99 100 101 |
# File 'lib/solr_cloud/collection.rb', line 99 def aliases connection.alias_map.select { |_alias_name, ac| ac.collection.name == name }.values.map(&:alias) end |
#alive? ⇒ Boolean
Check to see if the collection is alive
71 72 73 74 75 |
# File 'lib/solr_cloud/collection.rb', line 71 def alive? connection.get("solr/#{name}/admin/ping").body["status"] rescue Faraday::ResourceNotFound false end |
#commit(hard: false) ⇒ Object
Send a commit (soft if unspecified)
141 142 143 144 145 146 147 148 |
# File 'lib/solr_cloud/collection.rb', line 141 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?
152 153 154 |
# File 'lib/solr_cloud/collection.rb', line 152 def configset Configset.new(name: info["configName"], connection: connection) end |
#count ⇒ Fixnum
Get a count of how many documents are in the index
175 176 177 |
# File 'lib/solr_cloud/collection.rb', line 175 def count get("solr/#{name}/select", q: "*:*").body["response"]["numFound"] end |
#delete ⇒ Object
Forwarded on to the underlying SolrCloud connection
28 |
# File 'lib/solr_cloud/collection.rb', line 28 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)
56 57 58 59 60 61 |
# File 'lib/solr_cloud/collection.rb', line 56 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?
65 66 67 |
# File 'lib/solr_cloud/collection.rb', line 65 def exist? connection.only_collection_names.include?(name) end |
#get ⇒ Object
Forwarded on to the underlying SolrCloud connection
18 |
# File 'lib/solr_cloud/collection.rb', line 18 def_delegator :@connection, :get |
#get_alias(alias_name) ⇒ Alias?
Get an alias that points to this collection by name, or nil if not found
126 127 128 129 |
# File 'lib/solr_cloud/collection.rb', line 126 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
119 120 121 |
# File 'lib/solr_cloud/collection.rb', line 119 def has_alias?(alias_name) alias_names.include?(alias_name) end |
#healthy? ⇒ Boolean
Reported as healthy?
93 94 95 |
# File 'lib/solr_cloud/collection.rb', line 93 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.
87 88 89 |
# File 'lib/solr_cloud/collection.rb', line 87 def info connection.get("api/collections/#{name}").body["cluster"]["collections"][name] end |
#inspect ⇒ Object Also known as: to_s
179 180 181 182 183 184 185 186 187 |
# File 'lib/solr_cloud/collection.rb', line 179 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
23 |
# File 'lib/solr_cloud/collection.rb', line 23 def_delegator :@connection, :post |
#pretty_print(q) ⇒ Object
191 192 193 |
# File 'lib/solr_cloud/collection.rb', line 191 def pretty_print(q) q.text inspect end |
#put ⇒ Object
Forwarded on to the underlying SolrCloud connection
33 |
# File 'lib/solr_cloud/collection.rb', line 33 def_delegator :@connection, :put |