diff --git a/docarray/array/queryset/lookup.py b/docarray/array/queryset/lookup.py index b31a503b822..b41643914a3 100644 --- a/docarray/array/queryset/lookup.py +++ b/docarray/array/queryset/lookup.py @@ -28,6 +28,8 @@ """ from typing import TYPE_CHECKING +from docarray.document.data import _is_not_empty + if TYPE_CHECKING: from docarray import Document import re @@ -121,7 +123,7 @@ def lookup(key, val, doc: 'Document') -> bool: return is_empty != val else: - return (get_key in doc.non_empty_fields) == val + return (_is_not_empty(get_key, value)) == val else: # return value == val raise ValueError( diff --git a/docarray/document/data.py b/docarray/document/data.py index 6b033da23a0..c6e4f53f3ea 100644 --- a/docarray/document/data.py +++ b/docarray/document/data.py @@ -35,6 +35,27 @@ _all_mime_types = set(mimetypes.types_map.values()) +def _is_not_empty(attribute, value): + if value is not None: + if attribute not in default_values: + return True + else: + dv = default_values[attribute] + if dv in ( + 'ChunkArray', + 'MatchArray', + 'DocumentArray', + list, + dict, + 'Dict[str, NamedScore]', + ): + if value: + return True + elif value != dv: + return True + return False + + @dataclass(unsafe_hash=True, eq=False) class DocumentData: _reference_doc: 'Document' = field(hash=False, compare=False) @@ -67,23 +88,8 @@ def _non_empty_fields(self) -> Tuple[str]: f_name = f.name if not f_name.startswith('_') or f_name == '_metadata': v = getattr(self, f_name) - if v is not None: - if f_name not in default_values: - r.append(f_name) - else: - dv = default_values[f_name] - if dv in ( - 'ChunkArray', - 'MatchArray', - 'DocumentArray', - list, - dict, - 'Dict[str, NamedScore]', - ): - if v: - r.append(f_name) - elif v != dv: - r.append(f_name) + if _is_not_empty(f_name, v): + r.append(f_name) return tuple(r)