File tree Expand file tree Collapse file tree 4 files changed +48
-13
lines changed
llama_index/core/vector_stores
llama-index-integrations/vector_stores/llama-index-vector-stores-qdrant/llama_index/vector_stores/qdrant Expand file tree Collapse file tree 4 files changed +48
-13
lines changed Original file line number Diff line number Diff line change @@ -91,12 +91,19 @@ def _process_filter_match(
91
91
filter_matches_list = []
92
92
for filter_ in filter_list :
93
93
filter_matches = True
94
-
95
- filter_matches = _process_filter_match (
96
- operator = filter_ .operator ,
97
- value = filter_ .value ,
98
- metadata_value = metadata .get (filter_ .key , None ),
99
- )
94
+ metadata_value = metadata .get (filter_ .key , None )
95
+ if filter_ .operator == FilterOperator .IS_EMPTY :
96
+ filter_matches = (
97
+ metadata_value is None
98
+ or metadata_value == ""
99
+ or metadata_value == []
100
+ )
101
+ else :
102
+ filter_matches = _process_filter_match (
103
+ operator = filter_ .operator ,
104
+ value = filter_ .value ,
105
+ metadata_value = metadata_value ,
106
+ )
100
107
101
108
filter_matches_list .append (filter_matches )
102
109
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ class FilterOperator(str, Enum):
74
74
ALL = "all" # Contains all (array of strings)
75
75
TEXT_MATCH = "text_match" # full text match (allows you to search for a specific substring, token or phrase within the text field)
76
76
CONTAINS = "contains" # metadata array contains value (string or number)
77
+ IS_EMPTY = "is_empty" # the field is not exist or empty (null or empty array)
77
78
78
79
79
80
class FilterCondition (str , Enum ):
@@ -94,13 +95,15 @@ class MetadataFilter(BaseModel):
94
95
"""
95
96
96
97
key : str
97
- value : Union [
98
- StrictInt ,
99
- StrictFloat ,
100
- StrictStr ,
101
- List [StrictStr ],
102
- List [StrictFloat ],
103
- List [StrictInt ],
98
+ value : Optional [
99
+ Union [
100
+ StrictInt ,
101
+ StrictFloat ,
102
+ StrictStr ,
103
+ List [StrictStr ],
104
+ List [StrictFloat ],
105
+ List [StrictInt ],
106
+ ]
104
107
]
105
108
operator : FilterOperator = FilterOperator .EQ
106
109
Original file line number Diff line number Diff line change @@ -400,6 +400,23 @@ def test_query_with_all_filter_returns_matches(self) -> None:
400
400
assert result .ids is not None
401
401
self .assertEqual (len (result .ids ), 2 )
402
402
403
+ def test_query_with_is_empty_filter_returns_matches (self ) -> None :
404
+ simple_vector_store = SimpleVectorStore ()
405
+ simple_vector_store .add (_node_embeddings_for_test ())
406
+
407
+ filters = MetadataFilters (
408
+ filters = [
409
+ MetadataFilter (
410
+ key = "not_existed_key" , operator = FilterOperator .IS_EMPTY , value = None
411
+ )
412
+ ]
413
+ )
414
+ query = VectorStoreQuery (
415
+ query_embedding = [1.0 , 1.0 ], filters = filters , similarity_top_k = 3
416
+ )
417
+ result = simple_vector_store .query (query )
418
+ self .assertEqual (len (result .ids ), len (_node_embeddings_for_test ()))
419
+
403
420
def test_clear (self ) -> None :
404
421
simple_vector_store = SimpleVectorStore ()
405
422
simple_vector_store .add (_node_embeddings_for_test ())
Original file line number Diff line number Diff line change 44
44
MatchText ,
45
45
MatchValue ,
46
46
Payload ,
47
+ PayloadField ,
47
48
Range ,
48
49
HasIdCondition ,
50
+ IsEmptyCondition ,
49
51
)
50
52
51
53
logger = logging .getLogger (__name__ )
@@ -1095,6 +1097,12 @@ def _build_subfilter(self, filters: MetadataFilters) -> Filter:
1095
1097
match = MatchExcept (** {"except" : values }),
1096
1098
)
1097
1099
)
1100
+ elif subfilter .operator == FilterOperator .IS_EMPTY :
1101
+ # This condition will match all records where the field reports either does not exist, or has null or [] value.
1102
+ # https://qdrant.tech/documentation/concepts/filtering/#is-empty
1103
+ conditions .append (
1104
+ IsEmptyCondition (is_empty = PayloadField (key = subfilter .key ))
1105
+ )
1098
1106
1099
1107
filter = Filter ()
1100
1108
if filters .condition == FilterCondition .AND :
You can’t perform that action at this time.
0 commit comments