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



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

#connectionObject (readonly)

Returns The name of the get_collection.

Returns:

  • The name of the get_collection



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

def connection
  @connection
end

#nameSolrCloud::Connection (readonly)

Returns the underlying SolrCloud connection.

Returns:



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

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



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

Returns:

  • (Boolean)


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

Parameters:

  • alias_name (String)

    name of the alias to create

  • force (Boolean) (defaults to: true)

    whether or not to overwrite an existing alias

Returns:



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

The names of the aliases that point to this collection

Returns:

  • (Array<String>)

    the alias names



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?

Returns:

  • (Boolean)

    whether or not it has an alias



125
126
127
# File 'lib/solr_cloud/collection.rb', line 125

def aliased?
  !aliases.empty?
end

#aliasesArray<SolrCloud::Alias>

A (possibly empty) list of aliases targeting this collection

Returns:



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

Returns:

  • (Boolean)


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

#collectionObject



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)

Returns:

  • self



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

#configsetSolrCloud::ConfigSet

What configset was this created with?

Returns:

  • (SolrCloud::ConfigSet)


173
174
175
# File 'lib/solr_cloud/collection.rb', line 173

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



196
197
198
# File 'lib/solr_cloud/collection.rb', line 196

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

#deleteObject

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)

Returns:

  • (Connection)

    The connection object used to create this collection object

Raises:



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?

Returns:

  • (Boolean)


77
78
79
# File 'lib/solr_cloud/collection.rb', line 77

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

#getObject

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

Parameters:

  • alias_name (String)

    name of the alias

Returns:

  • (Alias, nil)

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

Parameters:

  • alias_name (String)

    name of the alias

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


107
108
109
# File 'lib/solr_cloud/collection.rb', line 107

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.



101
102
103
# File 'lib/solr_cloud/collection.rb', line 101

def info
  connection.get("api/collections/#{name}").body["cluster"]["collections"][name]
end

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

#postObject

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

#putObject

Forwarded on to the underlying SolrCloud connection



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

def_delegator :@connection, :put