Module: SolrCloud::Connection::CollectionAdmin

Included in:
SolrCloud::Connection
Defined in:
lib/solr_cloud/connection/collection_admin.rb

Overview

methods having to do with collections, to be included by the connection object. These are split out only to make it easier to deal with them.

For almost everything in here, we treat aliases like collections – calls to #collections, #has_collection?, #collection, etc. will respond to, and return, and alias if there is one. The idea is that you shouldn’t need to know if something is an alias or a collection until it’s relevant

Instance Method Summary collapse

Instance Method Details

#collection_namesArray<String>

A list of the names of existing collections and aliases

Returns:

  • (Array<String>)

    the collection/alias names, or empty array if there are none



68
69
70
# File 'lib/solr_cloud/connection/collection_admin.rb', line 68

def collection_names
  collections.map(&:name)
end

#collectionsArray<Collection, Alias>

Get a list of collections (and aliases)

Returns:

  • (Array<Collection, Alias>)

    possibly empty list of collection and alias objects



62
63
64
# File 'lib/solr_cloud/connection/collection_admin.rb', line 62

def collections
  only_collections.union(aliases)
end

#create_collection(name:, configset:, shards: 1, replication_factor: 1) ⇒ Collection

Create and return a new collection.

Parameters:

  • name (String)

    Name for the new collection

  • configset (String, Configset)

    (name of) the configset to use for this collection

  • shards (Integer) (defaults to: 1)
  • replication_factor (Integer) (defaults to: 1)

Returns:

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solr_cloud/connection/collection_admin.rb', line 22

def create_collection(name:, configset:, shards: 1, replication_factor: 1)

  unless legal_solr_name?(name)
    raise IllegalNameError.new("'#{name}' is not a valid solr name. Use only ASCII letters/numbers, dash, and underscore")
  end

  configset_name = case configset
                     when Configset
                       configset.name
                     else
                       configset.to_s
                   end
  raise WontOverwriteError.new("Collection #{name} already exists") if has_collection?(name)
  raise NoSuchConfigSetError.new("Configset '#{configset_name}' doesn't exist") unless has_configset?(configset_name)

  args = {
    :action => "CREATE",
    :name => name,
    :numShards => shards,
    :replicationFactor => replication_factor,
    "collection.configName" => configset_name
  }
  connection.get("solr/admin/collections", args)
  get_collection(name)
end

#get_collection(collection_name) ⇒ Collection, ...

Get a collection object specifically for the named collection

Parameters:

  • collection_name (String)

    name of the (already existing) collection

Returns:

  • (Collection, Alias, nil)

    The collection or alias if found, nil if not



81
82
83
84
85
86
87
88
# File 'lib/solr_cloud/connection/collection_admin.rb', line 81

def get_collection(collection_name)
  return nil unless has_collection?(collection_name)
  if only_collection_names.include?(collection_name)
    Collection.new(name: collection_name, connection: self)
  else
    get_alias(collection_name)
  end
end

#get_collection!(collection_name) ⇒ Collection, Alias

Get a connection/alias object, throwing an error if it’s not found

Parameters:

  • collection_name (String)

    name of the (already existing) collection

Returns:

Raises:



94
95
96
97
# File 'lib/solr_cloud/connection/collection_admin.rb', line 94

def get_collection!(collection_name)
  raise NoSuchCollectionError.new("Collection '#{collection_name}' not found") unless has_collection?(collection_name)
  get_collection(collection_name)
end

#has_collection?(name) ⇒ Boolean

Returns Whether a collection with the passed name exists.

Parameters:

  • name (String)

    name of the collection to check on

Returns:

  • (Boolean)

    Whether a collection with the passed name exists



74
75
76
# File 'lib/solr_cloud/connection/collection_admin.rb', line 74

def has_collection?(name)
  collection_names.include? name
end

#only_collection_namesArray<String>

The names of only connections (and not aliases). Useful as a utility.

Returns:

  • (Array<String>)

    the names of the connections



56
57
58
# File 'lib/solr_cloud/connection/collection_admin.rb', line 56

def only_collection_names
  only_collections.map(&:name)
end

#only_collectionsObject

Get a list of only collections, as opposed to the mix of collections and aliases we usually do.



50
51
52
# File 'lib/solr_cloud/connection/collection_admin.rb', line 50

def only_collections
  connection.get("api/collections").body["collections"].map { |c| Collection.new(name: c, connection: self) }
end