Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limit parameter to client.data_object.get() REST query #181

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion integration/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from datetime import datetime
from datetime import timezone
from typing import List
from typing import List, Optional

import pytest

Expand Down Expand Up @@ -78,6 +78,7 @@ def test_load_scheme(people_schema):
@pytest.fixture(scope="module")
def client(people_schema):
client = weaviate.Client("http://localhost:8080")
client.schema.delete_all()
client.schema.create(people_schema)

yield client
Expand All @@ -104,6 +105,25 @@ def test_timeout(people_schema, timeout):
client.schema.delete_all()


@pytest.mark.parametrize("limit", [None, 1, 5, 20, 50])
def test_query_get_with_limit(people_schema, limit: Optional[int]):
client = weaviate.Client("http://localhost:8080")
client.schema.delete_all()
client.schema.create(people_schema)

num_objects = 20
for i in range(num_objects):
with client.batch as batch:
batch.add_data_object({"name": f"name{i}"}, "Person")
batch.flush()
result = client.data_object.get(class_name="Person", limit=limit)
if limit is None or limit >= num_objects:
assert len(result["objects"]) == num_objects
else:
assert len(result["objects"]) == limit
client.schema.delete_all()


def test_query_data(client):
expected_name = "Sophie Scholl"
client.data_object.create(
Expand Down
28 changes: 1 addition & 27 deletions weaviate/batch/crud_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ..util import (
_capitalize_first_letter,
check_batch_result,
_check_positive_num,
)
from ..warnings import _Warnings

Expand Down Expand Up @@ -1521,33 +1522,6 @@ def connection_error_retries(self, value: int) -> None:
self._connection_error_retries = value


def _check_positive_num(value: Real, arg_name: str, data_type: type) -> None:
"""
Check if the `value` of the `arg_name` is a positive number.

Parameters
----------
value : Union[int, float]
The value to check.
arg_name : str
The name of the variable from the original function call. Used for error message.
data_type : type
The data type to check for.

Raises
------
TypeError
If the `value` is not of type `data_type`.
ValueError
If the `value` has a non positive value.
"""

if not isinstance(value, data_type) or isinstance(value, bool):
raise TypeError(f"'{arg_name}' must be of type {data_type}.")
if value <= 0:
raise ValueError(f"'{arg_name}' must be positive, i.e. greater that zero (>0).")


def _check_non_negative(value: Real, arg_name: str, data_type: type) -> None:
"""
Check if the `value` of the `arg_name` is a non-negative number.
Expand Down
12 changes: 10 additions & 2 deletions weaviate/data/crud_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_vector,
get_valid_uuid,
_capitalize_first_letter,
_check_positive_num,
)


Expand Down Expand Up @@ -417,6 +418,7 @@ def get(
additional_properties: List[str] = None,
with_vector: bool = False,
class_name: Optional[str] = None,
limit: Optional[int] = None,
) -> List[dict]:
"""
Gets objects from weaviate, the maximum number of objects returned is 100.
Expand All @@ -433,11 +435,14 @@ def get(
with_vector: bool
If True the `vector` property will be returned too,
by default False
class_name : Optional[str], optional
class_name: Optional[str], optional
The class name of the object with UUID `uuid`. Introduced in Weaviate version v1.14.0.
STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
by default None
limit: Optional[int], optional
The maximum number of data objects to return.
by default None, which uses the weaviate default of 100 entries

Returns
-------
Expand All @@ -455,7 +460,6 @@ def get(
weaviate.UnexpectedStatusCodeException
If weaviate reports a none OK status.
"""

is_server_version_14 = self._connection.server_version >= "1.14"

if class_name is None and is_server_version_14 and uuid is not None:
Expand Down Expand Up @@ -488,6 +492,10 @@ def get(
if uuid is not None:
path += "/" + get_valid_uuid(uuid)

if limit is not None:
_check_positive_num(limit, "limit", int)
params["limit"] = limit

try:
response = self._connection.get(
path=path,
Expand Down
28 changes: 28 additions & 0 deletions weaviate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import uuid as uuid_lib
from io import BufferedReader
from numbers import Real
from typing import Union, Sequence, Any, Optional

import requests
Expand Down Expand Up @@ -488,3 +489,30 @@ def check_batch_result(results: dict) -> None:
if "result" in result and "errors" in result["result"]:
if "error" in result["result"]["errors"]:
print(result["result"]["errors"])


def _check_positive_num(value: Real, arg_name: str, data_type: type) -> None:
"""
Check if the `value` of the `arg_name` is a positive number.

Parameters
----------
value : Union[int, float]
The value to check.
arg_name : str
The name of the variable from the original function call. Used for error message.
data_type : type
The data type to check for.

Raises
------
TypeError
If the `value` is not of type `data_type`.
ValueError
If the `value` has a non positive value.
"""

if not isinstance(value, data_type) or isinstance(value, bool):
raise TypeError(f"'{arg_name}' must be of type {data_type}.")
if value <= 0:
raise ValueError(f"'{arg_name}' must be positive, i.e. greater that zero (>0).")