Class: Weaviate::Collections

Inherits:
Base
  • Object
show all
Defined in:
lib/weaviate/collections.rb,
sig/weaviate.rbs

Overview

Manage collections (formerly known as "classes" in the Weaviate schema).

Constant Summary collapse

PATH =
"schema"

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Weaviate::Base

Instance Method Details

#add_property(class_name:, property:) ⇒ json

Add a property to an existing collection.

Parameters:

  • class_name: (String)
  • property: (Hash[untyped, untyped])

Returns:

  • (json)


161
162
163
164
165
166
167
168
169
170
# File 'lib/weaviate/collections.rb', line 161

def add_property(
  class_name:,
  property:
)
  response = client.connection.post("#{PATH}/#{class_name}/properties") do |req|
    req.body = property
  end

  response.body
end

#add_tenants(class_name:, tenants:) ⇒ json

Adds one or more tenants to a collection.

Parameters:

  • tenants (Array<String, Hash>) —

    Tenant names, or hashes such as { name: "a", activityStatus: "COLD" }

  • class_name: (String)
  • tenants: (Array[String | Hash[untyped, untyped]])

Returns:

  • (json)


106
107
108
109
110
111
112
113
114
# File 'lib/weaviate/collections.rb', line 106

def add_tenants(
  class_name:,
  tenants:
)
  response = client.connection.post("#{PATH}/#{class_name}/tenants") do |req|
    req.body = tenants_body(tenants)
  end
  response.body
end

#create(class_name:, description: nil, properties: nil, multi_tenant: false, auto_tenant_creation: false, auto_tenant_activation: false, vector_index_type: nil, vector_index_config: nil, vectorizer: nil, module_config: nil, inverted_index_config: nil, replication_config: nil, vector_config: nil) ⇒ json

Create a new collection.

Parameters:

  • class_name: (String)
  • description: (String, nil) (defaults to: nil)
  • properties: (Array[Hash[untyped, untyped]], nil) (defaults to: nil)
  • multi_tenant: (Boolean) (defaults to: false)
  • auto_tenant_creation: (Boolean) (defaults to: false)
  • auto_tenant_activation: (Boolean) (defaults to: false)
  • vector_index_type: (String, nil) (defaults to: nil)
  • vector_index_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • vectorizer: (String, nil) (defaults to: nil)
  • module_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • inverted_index_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • replication_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • vector_config: (Hash[untyped, untyped], nil) (defaults to: nil)

Returns:

  • (json)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weaviate/collections.rb', line 23

def create(
  class_name:,
  description: nil,
  properties: nil,
  multi_tenant: false,
  auto_tenant_creation: false,
  auto_tenant_activation: false,
  vector_index_type: nil,
  vector_index_config: nil,
  vectorizer: nil,
  module_config: nil,
  inverted_index_config: nil,
  replication_config: nil,
  vector_config: nil
)
  response = client.connection.post(PATH) do |req|
    req.body = {}
    req.body["class"] = class_name
    req.body["description"] = description unless description.nil?
    req.body["vectorIndexType"] = vector_index_type unless vector_index_type.nil?
    req.body["vectorIndexConfig"] = vector_index_config unless vector_index_config.nil?
    req.body["vectorizer"] = vectorizer unless vectorizer.nil?
    req.body["moduleConfig"] = module_config unless module_config.nil?
    req.body["properties"] = properties unless properties.nil?

    if multi_tenant
      req.body["multiTenancyConfig"] = {
        enabled: multi_tenant,
        autoTenantCreation: auto_tenant_creation,
        autoTenantActivation: auto_tenant_activation
      }
    end

    req.body["invertedIndexConfig"] = inverted_index_config unless inverted_index_config.nil?
    req.body["replicationConfig"] = replication_config unless replication_config.nil?
    req.body["vectorConfig"] = vector_config unless vector_config.nil?
  end

  response.body
end

#delete(class_name:) ⇒ Boolean

Remove a collection (and all data in the instances).

Parameters:

  • class_name: (String)

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/weaviate/collections.rb', line 65

def delete(class_name:)
  response = client.connection.delete("#{PATH}/#{class_name}")
  response.body.empty?
end

#get(class_name:) ⇒ json

Get a single collection

Parameters:

  • class_name: (String)

Returns:

  • (json)


17
18
19
20
# File 'lib/weaviate/collections.rb', line 17

def get(class_name:)
  response = client.connection.get("#{PATH}/#{class_name}")
  response.body
end

#get_tenant(class_name:, tenant:) ⇒ json

Get a single tenant of a collection.

Parameters:

  • class_name: (String)
  • tenant: (String)

Returns:

  • (json)


129
130
131
132
# File 'lib/weaviate/collections.rb', line 129

def get_tenant(class_name:, tenant:)
  response = client.connection.get("#{PATH}/#{class_name}/tenants/#{tenant}")
  response.body
end

#list ⇒ json

Lists all collections in the Weaviate schema.

Returns:

  • (json)


11
12
13
14
# File 'lib/weaviate/collections.rb', line 11

def list
  response = client.connection.get(PATH)
  response.body
end

#list_tenants(class_name:) ⇒ json

List tenants of a collection.

Parameters:

  • class_name: (String)

Returns:

  • (json)


143
144
145
146
# File 'lib/weaviate/collections.rb', line 143

def list_tenants(class_name:)
  response = client.connection.get("#{PATH}/#{class_name}/tenants")
  response.body
end

#remove_tenants(class_name:, tenants:) ⇒ json

Remove one or more tenants from a collection.

Parameters:

  • class_name: (String)
  • tenants: (Array[String])

Returns:

  • (json)


149
150
151
152
153
154
155
156
157
158
# File 'lib/weaviate/collections.rb', line 149

def remove_tenants(
  class_name:,
  tenants:
)
  response = client.connection.delete("#{PATH}/#{class_name}/tenants") do |req|
    req.body = tenants
  end

  response.body
end

#shards(class_name:, tenant: nil) ⇒ json

Inspect the shards of a collection

Parameters:

  • class_name: (String)
  • tenant: (String, nil) (defaults to: nil)

Returns:

  • (json)


173
174
175
176
177
178
# File 'lib/weaviate/collections.rb', line 173

def shards(class_name:, tenant: nil)
  response = client.connection.get("#{PATH}/#{class_name}/shards") do |req|
    req.params["tenant"] = tenant unless tenant.nil?
  end
  response.body
end

#tenant_exists?(class_name:, tenant:) ⇒ Boolean

Check if a tenant exists.

Parameters:

  • class_name: (String)
  • tenant: (String)

Returns:

  • (Boolean)


135
136
137
138
139
140
# File 'lib/weaviate/collections.rb', line 135

def tenant_exists?(class_name:, tenant:)
  response = client.connection.head("#{PATH}/#{class_name}/tenants/#{tenant}")
  response.success?
rescue Weaviate::ResourceNotFound
  false
end

#update(class_name:, description: nil, vector_index_type: nil, vector_index_config: nil, vectorizer: nil, module_config: nil, properties: nil, inverted_index_config: nil, replication_config: nil, vector_config: nil) ⇒ json

Update settings of an existing collection. Weaviate replaces the whole collection definition on update, so the provided settings are merged into the current definition. Immutable settings and existing properties cannot be changed.

Parameters:

  • class_name: (String)
  • description: (String, nil) (defaults to: nil)
  • vector_index_type: (String, nil) (defaults to: nil)
  • vector_index_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • vectorizer: (String, nil) (defaults to: nil)
  • module_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • properties: (Array[Hash[untyped, untyped]], nil) (defaults to: nil)
  • inverted_index_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • replication_config: (Hash[untyped, untyped], nil) (defaults to: nil)
  • vector_config: (Hash[untyped, untyped], nil) (defaults to: nil)

Returns:

  • (json)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/weaviate/collections.rb', line 73

def update(
  class_name:,
  description: nil,
  vector_index_type: nil,
  vector_index_config: nil,
  vectorizer: nil,
  module_config: nil,
  properties: nil,
  inverted_index_config: nil,
  replication_config: nil,
  vector_config: nil
)
  changes = {
    "description" => description,
    "vectorIndexType" => vector_index_type,
    "vectorIndexConfig" => vector_index_config,
    "vectorizer" => vectorizer,
    "moduleConfig" => module_config,
    "properties" => properties,
    "invertedIndexConfig" => inverted_index_config,
    "replicationConfig" => replication_config,
    "vectorConfig" => vector_config
  }.compact

  response = client.connection.put("#{PATH}/#{class_name}") do |req|
    req.body = deep_merge(get(class_name: class_name), JSON.parse(changes.to_json))
  end

  response.body
end

#update_shard_status(class_name:, shard_name:, status:) ⇒ json

Update shard status

Parameters:

  • class_name: (String)
  • shard_name: (String)
  • status: (String)

Returns:

  • (json)


181
182
183
184
185
186
187
188
189
190
# File 'lib/weaviate/collections.rb', line 181

def update_shard_status(class_name:, shard_name:, status:)
  validate_status!(status)

  response = client.connection.put("#{PATH}/#{class_name}/shards/#{shard_name}") do |req|
    req.body = {}
    req.body["status"] = status
  end

  response.body
end

#update_tenants(class_name:, tenants:) ⇒ json

Update the activity status of one or more tenants.

Parameters:

  • tenants (Array<Hash>) —

    e.g. [{ name: "a", activityStatus: "COLD" }]

  • class_name: (String)
  • tenants: (Array[String | Hash[untyped, untyped]])

Returns:

  • (json)


118
119
120
121
122
123
124
125
126
# File 'lib/weaviate/collections.rb', line 118

def update_tenants(
  class_name:,
  tenants:
)
  response = client.connection.put("#{PATH}/#{class_name}/tenants") do |req|
    req.body = tenants_body(tenants)
  end
  response.body
end