Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

Commit afd38a3

Browse files
author
Joan Fontanals Martinez
committed
docs: add docstring to filter
1 parent 3ea2dcc commit afd38a3

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

docarray/utils/filter.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def filter(
11-
index: AnyDocumentArray,
11+
docs: AnyDocumentArray,
1212
query: Union[str, Dict, List[Dict]],
1313
) -> AnyDocumentArray:
1414
"""
@@ -19,9 +19,36 @@ def filter(
1919
2020
.. code-block:: python
2121
22-
TODO: fill it
22+
from docarray import DocumentArray, BaseDocument
23+
from docarray.documents import Text, Image
24+
from docarray.util.filter import filter
2325
24-
:param index: the index of Documents to filter in
26+
27+
class MyDocument(BaseDocument):
28+
caption: Text
29+
image: Image
30+
price: int
31+
32+
33+
docs = DocumentArray[MyDocument](
34+
[MyDocument(caption='A tiger in the jungle', image=Image(url='tigerphoto.png'), price=100),
35+
MyDocument(caption='A swimming turtle', image=Image(url='turtlepic.png'), price=50),
36+
MyDocument(caption='A couple birdwatching with binoculars', image=Image(url='binocularsphoto.png'), price=30)]
37+
)
38+
query = {
39+
'$and': {
40+
'image.url': {'$regex': 'photo'},
41+
'price': {'$lte': 50},
42+
}
43+
}
44+
45+
results = filter(docs, query)
46+
assert len(results) == 1
47+
assert results[0].price == 30
48+
assert results[0].caption == 'A couple birdwatching with binoculars'
49+
assert results[0].image.url == 'binocularsphoto.png'
50+
51+
:param docs: the DocumentArray where to apply the filter
2552
:param query: the query to filter by
2653
:return: A DocumentArray containing the Documents
2754
inside DocumentArray that fullfil the filter conditions
@@ -31,6 +58,6 @@ def filter(
3158
if query:
3259
query = query if not isinstance(query, str) else json.loads(query)
3360
parser = QueryParser(query)
34-
return DocumentArray(d for d in index if parser.evaluate(d))
61+
return DocumentArray(d for d in docs if parser.evaluate(d))
3562
else:
36-
return index
63+
return docs

tests/units/util/test_filter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,35 @@ def test_logic_filter(docs, dict_api):
232232
}
233233
)
234234
assert len(result) == 3
235+
236+
237+
@pytest.mark.parametrize('dict_api', [True, False])
238+
def test_from_docstring(dict_api):
239+
class MyDocument(BaseDocument):
240+
caption: Text
241+
image: Image
242+
price: int
243+
244+
docs = DocumentArray[MyDocument](
245+
[MyDocument(caption='A tiger in the jungle', image=Image(url='tigerphoto.png'), price=100),
246+
MyDocument(caption='A swimming turtle', image=Image(url='turtlepic.png'), price=50),
247+
MyDocument(caption='A couple birdwatching with binoculars', image=Image(url='binocularsphoto.png'), price=30)]
248+
)
249+
250+
query = {
251+
'$and': {
252+
'image.url': {'$regex': 'photo'},
253+
'price': {'$lte': 50},
254+
}
255+
}
256+
257+
if dict_api:
258+
method = lambda query: filter(docs, query)
259+
else:
260+
method = lambda query: filter(docs, json.dumps(query))
261+
262+
results = method(query)
263+
assert len(results) == 1
264+
assert results[0].price == 30
265+
assert results[0].caption == 'A couple birdwatching with binoculars'
266+
assert results[0].image.url == 'binocularsphoto.png'

0 commit comments

Comments
 (0)