Skip to content

Commit 63ee163

Browse files
committed
Support using alias to ingest data.
There were few checks verifying that the collection would exist before ingesting/updating/deleting data. However, if an alias exists it is not found when listing collections. Therefore, a better check had to be included, as well as some code to use the collection with the alias right before the data operation is about to take place (it's not possible to do collection modifications or getting config using an alias)
1 parent 658788e commit 63ee163

File tree

1 file changed

+76
-21
lines changed

1 file changed

+76
-21
lines changed

weaviate_cli/managers/data_manager.py

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,18 @@ def __import_json(
237237
file_name: str,
238238
cl: wvc.ConsistencyLevel,
239239
num_objects: Optional[int] = None,
240+
alias: Optional[str] = None,
240241
) -> int:
241242
counter = 0
242-
properties: List[wvc.Property] = collection.config.get().properties
243+
if alias is None:
244+
properties: List[wvc.Property] = collection.config.get().properties
245+
else:
246+
collection_from_alias = self.client.alias.get(alias_name=alias).collection
247+
properties: List[wvc.Property] = (
248+
self.client.collections.get(collection_from_alias)
249+
.config.get()
250+
.properties
251+
)
243252

244253
try:
245254
with (
@@ -442,13 +451,21 @@ def __ingest_data(
442451
uuid: Optional[str] = None,
443452
verbose: bool = False,
444453
multi_vector: bool = False,
454+
alias: Optional[str] = None,
445455
) -> Collection:
446456
if randomize:
447457
click.echo(f"Generating {num_objects} objects")
448458
start_time = time.time()
449459

450460
# Determine vector dimensions based on vectorizer
451-
config = collection.config.get()
461+
if alias is None:
462+
config = collection.config.get()
463+
else:
464+
collection_from_alias = self.client.alias.get(
465+
alias_name=alias
466+
).collection
467+
config = self.client.collections.get(collection_from_alias).config.get()
468+
452469
if not config.vectorizer and config.vector_config:
453470
# Named vectors
454471
named_vectors = list(config.vector_config.keys())
@@ -582,9 +599,11 @@ def __ingest_data(
582599
)
583600
return cl_collection
584601
else:
585-
click.echo(f"Importing {num_objects} objects from Movies dataset")
602+
click.echo(
603+
f"Importing {num_objects} objects from Movies dataset {'using alias' if alias else ''}"
604+
)
586605
num_objects_inserted = self.__import_json(
587-
collection, "movies.json", cl, num_objects
606+
collection, "movies.json", cl, num_objects, alias
588607
)
589608
print(
590609
f"Inserted {num_objects_inserted} objects into class '{collection.name}'"
@@ -608,11 +627,16 @@ def create_data(
608627
multi_vector: bool = CreateDataDefaults.multi_vector,
609628
) -> Collection:
610629

630+
alias = None
611631
if not self.client.collections.exists(collection):
612-
613-
raise Exception(
614-
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command"
615-
)
632+
alias_list = self.client.alias.list_all()
633+
if collection not in alias_list.keys():
634+
raise Exception(
635+
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command"
636+
)
637+
else:
638+
alias = collection
639+
collection = str(alias_list[collection].collection)
616640

617641
col: Collection = self.client.collections.get(collection)
618642
mt_enabled = col.config.get().multi_tenancy_config.enabled
@@ -681,7 +705,9 @@ def create_data(
681705
if tenant == "None":
682706
initial_length = len(col)
683707
collection = self.__ingest_data(
684-
collection=col,
708+
collection=(
709+
col if alias is None else self.client.collections.get(alias)
710+
),
685711
num_objects=limit,
686712
cl=cl_map[consistency_level],
687713
randomize=randomize,
@@ -690,6 +716,7 @@ def create_data(
690716
uuid=uuid,
691717
verbose=verbose,
692718
multi_vector=multi_vector,
719+
alias=alias,
693720
)
694721
after_length = len(col)
695722
else:
@@ -712,7 +739,11 @@ def create_data(
712739
initial_length = len(col.with_tenant(tenant))
713740
click.echo(f"Processing objects for tenant '{tenant}'")
714741
collection = self.__ingest_data(
715-
collection=col.with_tenant(tenant),
742+
collection=(
743+
col.with_tenant(tenant)
744+
if alias is None
745+
else self.client.collections.get(alias).with_tenant(tenant)
746+
),
716747
num_objects=limit,
717748
cl=cl_map[consistency_level],
718749
randomize=randomize,
@@ -721,6 +752,7 @@ def create_data(
721752
uuid=uuid,
722753
verbose=verbose,
723754
multi_vector=multi_vector,
755+
alias=alias,
724756
)
725757
after_length = len(col.with_tenant(tenant))
726758
if wait_for_indexing:
@@ -951,11 +983,16 @@ def update_data(
951983
verbose: bool = UpdateDataDefaults.verbose,
952984
) -> None:
953985

986+
alias = None
954987
if not self.client.collections.exists(collection):
955-
956-
raise Exception(
957-
f"Class '{collection}' does not exist in Weaviate. Create first using ./create_class.py"
958-
)
988+
alias_list = self.client.alias.list_all()
989+
if collection not in alias_list.keys():
990+
raise Exception(
991+
f"Class '{collection}' does not exist in Weaviate. Create first using ./create_class.py"
992+
)
993+
else:
994+
alias = collection
995+
collection = str(alias_list[collection].collection)
959996

960997
col: Collection = self.client.collections.get(collection)
961998
try:
@@ -975,6 +1012,8 @@ def update_data(
9751012
}
9761013

9771014
click.echo(f"Preparing to update {limit} objects into class '{col.name}'")
1015+
# Override collection if alias is provided
1016+
col = col if alias is None else self.client.collections.get(alias)
9781017
for tenant in tenants:
9791018
if tenant == "None":
9801019
ret = self.__update_data(
@@ -1091,10 +1130,16 @@ def delete_data(
10911130
verbose: bool = DeleteDataDefaults.verbose,
10921131
) -> None:
10931132

1133+
alias = None
10941134
if not self.client.collections.exists(collection):
1095-
print(
1096-
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
1097-
)
1135+
alias_list = self.client.alias.list_all()
1136+
if collection not in alias_list.keys():
1137+
raise Exception(
1138+
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
1139+
)
1140+
else:
1141+
alias = collection
1142+
collection = str(alias_list[collection].collection)
10981143

10991144
return 1
11001145

@@ -1116,6 +1161,9 @@ def delete_data(
11161161
else:
11171162
tenants = existing_tenants
11181163

1164+
# Override collection if alias is provided
1165+
col = col if alias is None else self.client.collections.get(alias)
1166+
11191167
for tenant in tenants:
11201168
if tenant == "None":
11211169
ret = self.__delete_data(
@@ -1216,11 +1264,16 @@ def query_data(
12161264
target_vector: Optional[str] = QueryDataDefaults.target_vector,
12171265
) -> None:
12181266

1267+
alias = None
12191268
if not self.client.collections.exists(collection):
1220-
1221-
raise Exception(
1222-
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
1223-
)
1269+
alias_list = self.client.alias.list_all()
1270+
if collection not in alias_list.keys():
1271+
raise Exception(
1272+
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
1273+
)
1274+
else:
1275+
alias = collection
1276+
collection = str(alias_list[collection].collection)
12241277

12251278
col: Collection = self.client.collections.get(collection)
12261279
mt_enabled = col.config.get().multi_tenancy_config.enabled
@@ -1250,6 +1303,8 @@ def query_data(
12501303
"all": wvc.ConsistencyLevel.ALL,
12511304
"one": wvc.ConsistencyLevel.ONE,
12521305
}
1306+
# Override collection if alias is provided
1307+
col = col if alias is None else self.client.collections.get(alias)
12531308

12541309
for tenant in existing_tenants:
12551310
if tenant == "None":

0 commit comments

Comments
 (0)