Class: SolrCloud::Collection

Inherits:
Object
  • Object
show all
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

Alias

Instance Attribute Summary collapse

Instance Method Summary collapse

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”)

Parameters:

  • name (String)

    The name of the (already existing) collection

  • connection (SolrCloud::Connection)

    Connection to the solr “root” (blah:8888/)



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

#connectionObject (readonly)

Returns The name of the get_collection.

Returns:

  • The name of the get_collection



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

def connection
  @connection
end

#nameSolrCloud::Connection (readonly)

Returns the underlying SolrCloud connection.

Returns:



36
37
38
# File 'lib/solr_cloud/collection.rb', line 36

def name
  @name
end

Instance Method Details

#add(docs) ⇒ Object

TODO:

Gotta check for errors and such!

Add a document or array of documents

Parameters:

  • docs (Hash, Array<Hash>)

    One or more documents to add

Returns:

  • self



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

Returns:

  • (Boolean)


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

Parameters:

  • alias_name (String)

    name of the alias to create

  • force (Boolean) (defaults to: true)

    whether or not to overwrite an existing alias

Returns:



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_namesArray<String>

The names of the aliases that point to this collection

Returns:

  • (Array<String>)

    the alias names



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?

Returns:

  • (Boolean)

    whether or not it has an alias



111
112
113
# File 'lib/solr_cloud/collection.rb', line 111

def aliased?
  !aliases.empty?
end

#aliasesArray<SolrCloud::Alias>

A (possibly empty) list of aliases targeting this collection

Returns:



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

Returns:

  • (Boolean)


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)

Returns:

  • self



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

#configsetSolrCloud::ConfigSet

What configset was this created with?

Returns:

  • (SolrCloud::ConfigSet)


152
153
154
# File 'lib/solr_cloud/collection.rb', line 152

def configset
  Configset.new(name: info["configName"], connection: connection)
end

#countFixnum

Get a count of how many documents are in the index

Returns:

  • (Fixnum)

    the number of documents



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

def count
  get("solr/#{name}/select", q: "*:*").body["response"]["numFound"]
end

#deleteObject

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)

Returns:

  • (Connection)

    The connection object used to create this collection object

Raises:



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?

Returns:

  • (Boolean)


65
66
67
# File 'lib/solr_cloud/collection.rb', line 65

def exist?
  connection.only_collection_names.include?(name)
end

#getObject

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

Parameters:

  • alias_name (String)

    name of the alias

Returns:

  • (Alias, nil)

    The alias object, 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

Parameters:

  • alias_name (String)

    name of the alias

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


93
94
95
# File 'lib/solr_cloud/collection.rb', line 93

def healthy?
  info["health"] == "GREEN"
end

#infoObject

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

#inspectObject 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

#postObject

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

#putObject

Forwarded on to the underlying SolrCloud connection



33
# File 'lib/solr_cloud/collection.rb', line 33

def_delegator :@connection, :put