Multi-tenancy operations
Enable multi-tenancy
- Multi-tenancy available from version
v1.20
- (Experimental) Tenant activity status setting available from version
v1.21
Multi-tenancy is disabled by default. To enable it, set the multiTenancyConfig
variable in the class definition as shown below:
{
"class": "MultiTenancyClass",
"multiTenancyConfig": {"enabled": true}
}
Class operations
Add tenant(s)
To add tenants to a class, you must provide the tenant names to the Weaviate class. From 1.21
onwards, you can also optionally specify whether the tenant is to be active (HOT
, default), or inactive (COLD
).
A tenant name can only contain alphanumeric characters (a-z, A-Z, 0-9), underscore (_), and hyphen (-), with a length of 4 to 64 characters.
Code examples are shown below in which the tenants tenantA
and tenantB
are added to the class MultiTenancyClass
:
- Python
- JavaScript/TypeScript
- Java
- Go
from weaviate import Tenant
client.schema.create_class({
'class': 'MultiTenancyClass',
'multiTenancyConfig': {'enabled': True}
})
client.schema.add_class_tenants(
class_name='MultiTenancyClass', # The class to which the tenants will be added
tenants=[Tenant(name='tenantA'), Tenant(name='tenantB')]
)
await client.schema
.classCreator().withClass({
class: 'MultiTenancyClass',
multiTenancyConfig: { enabled: true },
})
.do();
let tenants = await client.schema
.tenantsCreator('MultiTenancyClass', [{ name: 'tenantA' }, { name: 'tenantB' }])
.do();
WeaviateClass multiTenancyClass = WeaviateClass.builder()
.className("MultiTenancyClass")
.multiTenancyConfig(MultiTenancyConfig.builder().enabled(true).build())
.build();
client.schema().classCreator()
.withClass(multiTenancyClass)
.run();
client.schema().tenantsCreator()
.withClassName("MultiTenancyClass")
.withTenants(
Tenant.builder().name("tenantA").build(),
Tenant.builder().name("tenantB").build()
)
.run();
client.Schema().ClassCreator().
WithClass(&models.Class{
Class: "MultiTenancyClass",
MultiTenancyConfig: &models.MultiTenancyConfig{
Enabled: true,
},
}).
Do(ctx)
client.Schema().TenantsCreator().
WithClassName("MultiTenancyClass").
WithTenants(models.Tenant{Name: "tenantA"}, models.Tenant{Name: "tenantB"}).
Do(ctx)
List tenant(s)
To list existing tenants in a class, you must provide the Weaviate class name.
Code examples are shown below for listing the existing tenants in the MultiTenancyClass
class:
- Python
- JavaScript/TypeScript
- Java
- Go
tenants = client.schema.get_class_tenants(
class_name='MultiTenancyClass' # The class from which the tenants will be retrieved
)
tenants = await client.schema
.tenantsGetter('MultiTenancyClass')
.do();
client.schema().tenantsGetter()
.withClassName("MultiTenancyClass")
.run();
tenants, err := client.Schema().TenantsGetter().
WithClassName("MultiTenancyClass").
Do(ctx)
Delete tenant(s)
You can delete one or more existing tenants in a class by providing the Weaviate class name.
If a tenant specified for deletion doesn't belong to the class, it is ignored.
- Python
- JavaScript/TypeScript
- Java
- Go
client.schema.remove_class_tenants(
class_name='MultiTenancyClass', # The class from which the tenants will be removed
tenants=['tenantB', 'tenantX'] # The tenants to be removed. tenantX will be ignored.
)
await client.schema
.tenantsDeleter('MultiTenancyClass', ['tenantB', 'tenantX']) // tenantX will be ignored
.do();
client.schema().tenantsDeleter()
.withClassName("MultiTenancyClass")
.withTenants("tenantB", "tenantX") // tenantX will be ignored
.run();
client.Schema().TenantsDeleter().
WithClassName("MultiTenancyClass").
WithTenants("tenantB", "tenantX"). // tenantX will be ignored
Do(ctx)
Update tenant(s)
v1.21
You can update one or more existing tenants' activity status to active ("HOT") or inactive ("COLD").
For now, please send a PUT request through the REST API endpoint to update the tenant activity status.
Object operations
CRUD operations
If multi-tenancy is enabled, you must provide the tenant name to Weaviate in each CRUD operation.
Code examples are shown below for creating an object in the MultiTenancyClass
class:
- Python
- JavaScript/TypeScript
- Java
- Go
object_id = client.data_object.create(
class_name='MultiTenancyClass', # The class to which the object will be added
data_object={
'question': 'This vector DB is OSS & supports automatic property type inference on import'
},
tenant='tenantA' # The tenant to which the object will be added
)
let object = await client.data.creator()
.withClassName('MultiTenancyClass') // The class to which the object will be added
.withProperties({
question: 'This vector DB is OSS & supports automatic property type inference on import',
})
.withTenant('tenantA') // The tenant to which the object will be added
.do();
Result<WeaviateObject> result = client.data().creator()
.withClassName("MultiTenancyClass") // The class to which the object will be added
.withProperties(new HashMap<String, Object>() {{
put("question", "This vector DB is OSS & supports automatic property type inference on import");
}})
.withTenant("tenantA") // The tenant to which the object will be added
.run();
object, err := client.Data().Creator().
WithClassName("MultiTenancyClass"). // The class to which the object will be added
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS & supports automatic property type inference on import",
}).
WithTenant("tenantA"). // The tenant to which the object will be added
Do(ctx)
Search queries
Get
and Aggregate
queries support multi-tenancy operations. (Explore
queries do not support multi-tenancy operations at this point.)
If multi-tenancy is enabled, you must provide the tenant name to Weaviate in each search query.
Code examples are shown below for fetching one object in the MultiTenancyClass
class from the tenant tenantA
:
- Python
- JavaScript/TypeScript
- Java
- Go
result = (
client.query.get('MultiTenancyClass', ['question'])
.with_tenant('tenantA')
.do()
)
const result = await client.graphql
.get()
.withClassName('MultiTenancyClass')
.withFields('question')
.withTenant('tenantA')
.do();
Result<GraphQLResponse> result = client.graphQL().get()
.withClassName("MultiTenancyClass")
.withFields(Field.builder().name("question").build())
.withTenant("tenantA")
.run();
result, err := client.GraphQL().Get().
WithClassName("MultiTenancyClass").
WithFields(graphql.Field{Name: "question"}).
WithTenant("tenantA").
Do(ctx)
Cross-references
If multi-tenancy is enabled, you must provide the tenant name to Weaviate while creating, updating or deleting cross-references.
You can establish a cross-reference from a multi-tenancy class object to:
- A non-multi-tenancy class object, or
- A multi-tenancy class object belonging to the same tenant.
The example below creates a cross-reference between two objects. It links an object in the MultiTenancyClass
class that belongs to tenantA
, to an object in the JeopardyCategory
class:
- Python
- JavaScript/TypeScript
- Java
- Go
# Add the cross-reference property to the multi-tenancy class
client.schema.property.create('MultiTenancyClass', {
'name': 'hasCategory',
'dataType': ['JeopardyCategory'],
})
client.data_object.reference.add(
from_uuid=object_id, # MultiTenancyClass object id (a Jeopardy question)
from_class_name='MultiTenancyClass',
from_property_name='hasCategory',
tenant='tenantA',
to_class_name='JeopardyCategory',
to_uuid=category_id
)
// Add the cross-reference property to the multi-tenancy class
await client.schema.propertyCreator()
.withClassName('MultiTenancyClass')
.withProperty({
'name': 'hasCategory',
'dataType': ['JeopardyCategory'],
})
.do();
// Create the cross-reference from MultiTenancyClass object to the JeopardyCategory object
await client.data
.referenceCreator()
.withClassName('MultiTenancyClass')
.withTenant('tenantA')
.withId(object.id) // MultiTenancyClass object id (a Jeopardy question)
.withReferenceProperty('hasCategory')
.withReference(
client.data
.referencePayloadBuilder()
.withClassName('JeopardyCategory')
.withId(category.id)
.payload()
)
.do();
// Add the cross-reference property to the multi-tenancy class
client.schema().propertyCreator()
.withClassName("MultiTenancyClass")
.withProperty(Property.builder()
.name("hasCategory")
.dataType(Collections.singletonList("JeopardyCategory")).build()
)
.run();
// Create the cross-reference from MultiTenancyClass object to the JeopardyCategory object
client.data().referenceCreator()
.withClassName("MultiTenancyClass")
.withTenant("tenantA")
.withID(object.getId()) // MultiTenancyClass object id (a Jeopardy question)
.withReferenceProperty("hasCategory")
.withReference(
client.data()
.referencePayloadBuilder()
.withClassName("JeopardyCategory")
.withID(category.getId())
.payload()
)
.run();
// Add the cross-reference property to the multi-tenancy class
client.Schema().PropertyCreator().
WithClassName("MultiTenancyClass").
WithProperty(&models.Property{
Name: "hasCategory",
DataType: []string{"JeopardyCategory"},
}).
Do(ctx)
// Create the cross-reference from MultiTenancyClass object to the JeopardyCategory object
client.Data().ReferenceCreator().
WithClassName("MultiTenancyClass").
WithTenant("tenantA").
WithID(object.ID.String()). // MultiTenancyClass object id (a Jeopardy question)
WithReferenceProperty("hasCategory").
WithReference(client.Data().ReferencePayloadBuilder().
WithClassName("JeopardyCategory").
WithID(category.ID.String()).
Payload()).
Do(ctx)
As described above, the JeopardyCategory
class object can be either:
- A non-multi-tenancy object or
- A multi-tenancy object belonging to
tenantA
.
More Resources
For additional information, try these sources.