FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docarray/docarray/utils/filter.py at fix-num-docs · docarray/docarray · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
docarray
/
docarray
Public
Notifications
You must be signed in to change notification settings
Fork
243
Star
3.1k
Code
Issues
68
Pull requests
43
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
docarray
/
docarray
/
utils
/
filter.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (66 loc) · 2.13 KB
Breadcrumbs
docarray
/
docarray
/
utils
/
filter.py
Copy path
File metadata and controls
82 lines (66 loc) · 2.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
__all__
=
[
'filter_docs'
]
import
json
from
typing
import
Dict
,
List
,
Union
from
docarray
.
array
.
any_array
import
AnyDocArray
from
docarray
.
array
.
doc_list
.
doc_list
import
DocList
def
filter_docs
(
docs
:
AnyDocArray
,
query
:
Union
[
str
,
Dict
,
List
[
Dict
]],
)
->
AnyDocArray
:
"""
Filter the Documents in the index according to the given filter query.
---
```python
from docarray import DocList, BaseDoc
from docarray.documents import TextDoc, ImageDoc
from docarray.utils.filter import filter_docs
class MyDocument(BaseDoc):
caption: TextDoc
ImageDoc: ImageDoc
price: int
docs = DocList[MyDocument](
[
MyDocument(
caption='A tiger in the jungle',
ImageDoc=ImageDoc(url='tigerphoto.png'),
price=100,
),
MyDocument(
caption='A swimming turtle',
ImageDoc=ImageDoc(url='turtlepic.png'),
price=50,
),
MyDocument(
caption='A couple birdwatching with binoculars',
ImageDoc=ImageDoc(url='binocularsphoto.png'),
price=30,
),
]
)
query = {
'$and': {
'ImageDoc__url': {'$regex': 'photo'},
'price': {'$lte': 50},
}
}
results = filter_docs(docs, query)
assert len(results) == 1
assert results[0].price == 30
assert results[0].caption == 'A couple birdwatching with binoculars'
assert results[0].ImageDoc.url == 'binocularsphoto.png'
```
---
:param docs: the DocList where to apply the filter
:param query: the query to filter by
:return: A DocList containing the Documents
in `docs` that fulfill the filter conditions in the `query`
"""
from
docarray
.
utils
.
_internal
.
query_language
.
query_parser
import
QueryParser
if
query
:
query
=
query
if
not
isinstance
(
query
,
str
)
else
json
.
loads
(
query
)
parser
=
QueryParser
(
query
)
return
DocList
.
__class_getitem__
(
docs
.
doc_type
)(
d
for
d
in
docs
if
parser
.
evaluate
(
d
)
)
else
:
return
docs
Back
|
FazBrowse Home
|
New Git URL