Module: SolrCloud::Connection::ConfigsetAdmin

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

Overview

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

Defined Under Namespace

Classes: ZipFileGenerator

Instance Method Summary collapse

Instance Method Details

#configset_namesArray<String>

Get the names of all defined configsets.

The API endpoint differs across major Solr versions:

  • Solr 8: uses the V2 API (+GET /api/cluster/configs+).
  • Solr 9+: uses the V1 API (+GET /solr/admin/configs?action=LIST+). The V2 configset path changed between Solr 8 and Solr 9; V1 is used for consistency on Solr 9 and later.

Returns:

  • (Array<String>)

    the names of the config sets



72
73
74
75
76
77
78
# File 'lib/solr_cloud/connection/configset_admin.rb', line 72

def configset_names
  if solr9_or_later?
    connection.get("solr/admin/configs", action: "LIST").body["configSets"]
  else
    connection.get("api/cluster/configs").body["configSets"]
  end
end

#configsetsArray<Configset>

Get a list of the already-defined configsets.

Returns:

  • (Array<Configset>)

    possibly empty list of configsets



58
59
60
# File 'lib/solr_cloud/connection/configset_admin.rb', line 58

def configsets
  configset_names.map { |cs| Configset.new(name: cs, connection: self) }
end

#create_configset(name:, confdir:, force: false) ⇒ Configset

Given the path to a solr configuration "conf" directory (i.e., the one with solrconfig.xml in it), zip it up and send it to solr as a new configset.

The upload API endpoint differs across major Solr versions:

  • Solr 8: uses the V2 API (+PUT /api/cluster/configs/+), which was the canonical configset path in Solr 8.
  • Solr 9+: uses the V2 API (+PUT /api/configsets/+). The V2 configset path moved from /api/cluster/configs/<name> (Solr 8) to /api/configsets/<name> (Solr 9+). The new V2 PUT defaults to overwriting an existing configset, which means in-use configsets can be replaced without deleting them first.

Dispatches via SolrCloud::Connection#solr9_or_later?; see #upload_configset_v9 and #upload_configset_v8 for the version-specific implementations.

Parameters:

  • name (String)

    Name to give the new configset

  • confdir (String, Pathname)

    Path to the solr configuration "conf" directory

  • force (Boolean) (defaults to: false)

    Whether or not to overwrite an existing configset if there is one

Returns:

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/solr_cloud/connection/configset_admin.rb', line 32

def create_configset(name:, confdir:, force: false)
  config_set_name = name
  unless legal_solr_name?(config_set_name)
    raise IllegalNameError.new("'#{config_set_name}' is not a valid solr configset name. Use only ASCII letters/numbers, dash, and underscore")
  end

  if has_configset?(config_set_name) && !force
    raise WontOverwriteError.new("Won't replace configset #{config_set_name} unless 'force: true' passed ")
  end

  zfile = "#{Dir.tmpdir}/solr_add_configset_#{name}_#{Time.now.hash}.zip"
  ZipFileGenerator.new(confdir, zfile).write

  if solr9_or_later?
    upload_configset_v9(config_set_name, zfile)
  else
    upload_configset_v8(config_set_name, zfile)
  end

  get_configset(name)
ensure
  FileUtils.rm(zfile, force: true) if zfile
end

#delete_configset(name) ⇒ Connection

Remove the configuration set with the given name. No-op if the configset doesn't actually exist. Test with #has_configset? and SolrCloud::Configset#in_use? manually if need be.

In general, prefer using SolrCloud::Configset#delete! instead of running everything through the connection object.

The delete API endpoint differs across major Solr versions:

  • Solr 8: uses the V2 API (+DELETE /api/cluster/configs/+).
  • Solr 9+: uses the V1 API (+GET /solr/admin/configs?action=DELETE+). The V2 configset path changed between Solr 8 and Solr 9; V1 is used for consistency on Solr 9 and later.

Parameters:

  • name (String)

    The name of the configuration set

Returns:

Raises:

  • (ConfigSetInUseError)

    if the configset can't be deleted because it's in use by a live collection



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/solr_cloud/connection/configset_admin.rb', line 115

def delete_configset(name)
  if has_configset?(name)
    if solr9_or_later?
      connection.get("solr/admin/configs", action: "DELETE", name: name)
    else
      connection.delete("api/cluster/configs/#{name}")
    end
  end
  self
rescue Faraday::BadRequestError => e
  msg = e.response[:body]["error"]["msg"]
  if msg.match?(/not delete ConfigSet/)
    raise ConfigSetInUseError.new msg
  else
    raise e
  end
end

#get_configset(name) ⇒ Configset

Note:

Does not verify that the configset actually exists; use #has_configset? to check first if needed.

Get an existing configset by name.

Parameters:

  • name (String)

    Name of the configset

Returns:



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

def get_configset(name)
  Configset.new(name: name, connection: self)
end

#has_configset?(name) ⇒ Boolean

Check to see if a configset with the given name is defined.

Parameters:

  • name (String)

    Name of the configset

Returns:

  • (Boolean)

    Whether a configset with that name exists



83
84
85
# File 'lib/solr_cloud/connection/configset_admin.rb', line 83

def has_configset?(name)
  configset_names.include? name.to_s
end