Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 74a683c

Browse filesBrowse files
committed
feat: contain func for weaviate
Signed-off-by: maxwelljin2 <gejin@berkeley.edu>
1 parent 6ca3aa6 commit 74a683c
Copy full SHA for 74a683c

2 files changed

+38-50Lines changed: 38 additions & 50 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docarray/index/backends/weaviate.py‎

Copy file name to clipboardExpand all lines: docarray/index/backends/weaviate.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from docarray.typing import AnyTensor
3232
from docarray.typing.tensor.abstract_tensor import AbstractTensor
3333
from docarray.typing.tensor.ndarray import NdArray
34+
from docarray.utils._internal._typing import safe_issubclass
3435
from docarray.utils._internal.misc import import_library
3536
from docarray.utils.find import FindResult, _FindResult
3637

@@ -762,6 +763,25 @@ def _filter_by_parent_id(self, id: str) -> Optional[List[str]]:
762763
]
763764
return ids
764765

766+
def __contains__(self, item: BaseDoc) -> bool:
767+
if safe_issubclass(type(item), BaseDoc):
768+
result = (
769+
self._client.query.get(self.index_name, ['docarrayid'])
770+
.with_where(
771+
{
772+
"path": ['docarrayid'],
773+
"operator": "Equal",
774+
"valueString": f'{item.id}',
775+
}
776+
)
777+
.do()
778+
)
779+
return len(result["data"]["Get"][self.index_name]) > 0
780+
else:
781+
raise TypeError(
782+
f"item must be an instance of BaseDoc or its subclass, not '{type(item).__name__}'"
783+
)
784+
765785
class QueryBuilder(BaseDocIndex.QueryBuilder):
766786
def __init__(self, document_index):
767787
self._queries = [
Collapse file

‎tests/index/weaviate/test_find_weaviate.py‎

Copy file name to clipboardExpand all lines: tests/index/weaviate/test_find_weaviate.py
+18-50Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from docarray import BaseDoc
1010
from docarray.index.backends.weaviate import WeaviateDocumentIndex
11-
from docarray.typing import TorchTensor
11+
from docarray.typing import NdArray, TorchTensor
1212
from tests.index.weaviate.fixture_weaviate import ( # noqa: F401
1313
start_storage,
1414
weaviate_client,
@@ -68,55 +68,23 @@ class TfDoc(BaseDoc):
6868
)
6969

7070

71-
def test_comprehensive():
72-
import numpy as np
73-
from pydantic import Field
74-
75-
from docarray import BaseDoc
76-
from docarray.index.backends.weaviate import WeaviateDocumentIndex
77-
from docarray.typing import NdArray
78-
79-
class Document(BaseDoc):
80-
text: str
81-
embedding: NdArray[2] = Field(
82-
dims=2, is_embedding=True
83-
) # Embedding column -> vector representation of the document
84-
file: NdArray[100] = Field(dims=100)
85-
86-
docs = [
87-
Document(
88-
text="Hello world",
89-
embedding=np.array([1, 2]),
90-
file=np.random.rand(100),
91-
id="1",
92-
),
93-
Document(
94-
text="Hello world, how are you?",
95-
embedding=np.array([3, 4]),
96-
file=np.random.rand(100),
97-
id="2",
98-
),
99-
Document(
100-
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut",
101-
embedding=np.array([5, 6]),
102-
file=np.random.rand(100),
103-
id="3",
104-
),
105-
]
71+
def test_contain():
72+
class SimpleDoc(BaseDoc):
73+
tens: NdArray[10] = Field(dims=1000)
10674

107-
batch_config = {
108-
"batch_size": 20,
109-
"dynamic": False,
110-
"timeout_retries": 3,
111-
"num_workers": 1,
112-
}
75+
class SimpleSchema(BaseDoc):
76+
tens: NdArray[10]
11377

114-
dbconfig = WeaviateDocumentIndex.DBConfig(
115-
host="https://docarray-test-4mfexsso.weaviate.network", # Replace with your endpoint
116-
auth_api_key="JPsfPHB3OLHrgnN80JAa7bmPApOxOfaHy0SO",
117-
)
78+
index = WeaviateDocumentIndex[SimpleSchema]()
79+
index_docs = [SimpleDoc(tens=np.zeros(10)) for _ in range(10)]
80+
81+
assert (index_docs[0] in index) is False
82+
83+
index.index(index_docs)
84+
85+
for doc in index_docs:
86+
assert (doc in index) is True
11887

119-
runtimeconfig = WeaviateDocumentIndex.RuntimeConfig(batch_config=batch_config)
120-
store = WeaviateDocumentIndex[Document](db_config=dbconfig)
121-
store.configure(runtimeconfig) # Batch settings being passed on
122-
store.index(docs)
88+
index_docs_new = [SimpleDoc(tens=np.zeros(10)) for _ in range(10)]
89+
for doc in index_docs_new:
90+
assert (doc in index) is False

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.