Skip to content

Commit

Permalink
Merge pull request #89 from weaviate/rodrigo/handle_single_uuid
Browse files Browse the repository at this point in the history
Add single object delete and query
  • Loading branch information
jfrancoa authored Nov 6, 2024
2 parents 647e7df + 2d148df commit d0ecbeb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
7 changes: 6 additions & 1 deletion weaviate_cli/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ def delete_tenants_cli(
type=click.Choice(["quorum", "all", "one"]),
help="Consistency level (default: 'quorum').",
)
@click.option(
"--uuid",
help="UUID of the oject to be deleted. If provided, --limit will be ignored.",
)
@click.pass_context
def delete_data_cli(ctx, collection, limit, consistency_level):
def delete_data_cli(ctx, collection, limit, consistency_level, uuid):
"""Delete data from a collection in Weaviate."""

client = None
Expand All @@ -106,6 +110,7 @@ def delete_data_cli(ctx, collection, limit, consistency_level):
collection=collection,
limit=limit,
consistency_level=consistency_level,
uuid=uuid,
)
except Exception as e:
click.echo(f"Error: {e}")
Expand Down
2 changes: 1 addition & 1 deletion weaviate_cli/commands/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def query() -> None:
@click.option(
"--search_type",
default="fetch",
type=click.Choice(["fetch", "vector", "keyword", "hybrid"]),
type=click.Choice(["fetch", "vector", "keyword", "hybrid", "uuid"]),
help='Search type (default: "fetch").',
)
@click.option(
Expand Down
29 changes: 26 additions & 3 deletions weaviate_cli/managers/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,18 @@ def update_data(
)

def __delete_data(
self, collection: Collection, num_objects: int, cl: wvc.ConsistencyLevel
self,
collection: Collection,
num_objects: int,
cl: wvc.ConsistencyLevel,
uuid: Optional[str] = None,
) -> int:

if uuid:
collection.with_consistency_level(cl).data.delete_by_id(uuid=uuid)
print(f"Object deleted: {uuid} into class '{collection.name}'")
return 1

res = collection.query.fetch_objects(limit=num_objects)
if len(res.objects) == 0:
print(
Expand All @@ -357,7 +366,13 @@ def __delete_data(
print(f"Deleted {num_objects} objects into class '{collection.name}'")
return num_objects

def delete_data(self, collection: str, limit: int, consistency_level: str) -> None:
def delete_data(
self,
collection: str,
limit: int,
consistency_level: str,
uuid: Optional[str] = None,
) -> None:

if not self.client.collections.exists(collection):
print(
Expand Down Expand Up @@ -385,13 +400,14 @@ def delete_data(self, collection: str, limit: int, consistency_level: str) -> No

for tenant in tenants:
if tenant == "None":
ret = self.__delete_data(col, limit, cl_map[consistency_level])
ret = self.__delete_data(col, limit, cl_map[consistency_level], uuid)
else:
click.echo(f"Processing tenant '{tenant}'")
ret = self.__delete_data(
col.with_tenant(tenant),
limit,
cl_map[consistency_level],
uuid,
)
if ret == -1:

Expand Down Expand Up @@ -437,6 +453,13 @@ def __query_data(
return_metadata=MetadataQuery(score=True),
limit=num_objects,
)
elif search_type == "uuid":
# UUID logic
num_objects = 1
response = collection.with_consistency_level(cl).query.fetch_object_by_id(
uuid=query
)

else:
click.echo(
f"Invalid search type: {search_type}. Please choose from 'fetch', 'vector', 'keyword', or 'hybrid'."
Expand Down
24 changes: 15 additions & 9 deletions weaviate_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,34 @@ def get_random_string(length):
# Pretty print objects in the response in a table format
def pp_objects(response, main_properties):

if len(response.objects) == 0:
print("No objects found")
return

# Create the header
header = f"{'ID':<37}"
for prop in main_properties:
header += f"{prop.capitalize():<37}"
header += f"{'Distance':<11}{'Certainty':<11}{'Score':<11}"
print(header)

objects = []
if type(response) == weaviate.collections.classes.internal.ObjectSingleReturn:
objects.append(response)
else:
objects = response.objects

if len(objects) == 0:
print("No objects found")
return

# Print each object
for obj in response.objects:
for obj in objects:
row = f"{str(obj.uuid):<36} "
for prop in main_properties:
row += f"{str(obj.properties.get(prop, ''))[:36]:<36} "
row += f"{str(obj.metadata.distance)[:10] if obj.metadata.distance else 'None':<10} "
row += f"{str(obj.metadata.certainty)[:10] if obj.metadata.certainty else 'None':<10} "
row += f"{str(obj.metadata.score)[:10] if obj.metadata.score else 'None':<10}"
row += f"{str(obj.metadata.distance)[:10] if hasattr(obj.metadata, 'distance') else 'None':<10} "
row += f"{str(obj.metadata.certainty)[:10] if hasattr(obj.metadata, 'certainty') else 'None':<10} "
row += f"{str(obj.metadata.score)[:10] if hasattr(obj.metadata, 'score') else 'None':<10}"
print(row)

# Print footer
footer = f"{'':<37}" * (len(main_properties) + 1) + f"{'':<11}{'':<11}{'':<11}"
print(footer)
print(f"Total: {len(response.objects)} objects")
print(f"Total: {len(objects)} objects")

0 comments on commit d0ecbeb

Please sign in to comment.