Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(vectordb): exclude the subtree for must_not on a path inside an and (#4994)

alanhuangyoo466b62b604b6
diff --git a/src/index/detail/scalar/filter/filter_ops.cpp b/src/index/detail/scalar/filter/filter_ops.cpp
index 354d69cea..977ec6626 100644
--- a/src/index/detail/scalar/filter/filter_ops.cpp
+++ b/src/index/detail/scalar/filter/filter_ops.cpp
@@ -831,34 +831,24 @@ BitmapPtr MustNotOp::calc_self_bitmap(
BitmapPtr MustNotOp::calc_bitmap(FieldBitmapGroupSetPtr field_group_set_ptr,
BitmapPtr pres, const std::string on_res_op) {
if (!pres) {
- pres = calc_self_bitmap(field_group_set_ptr);
- } else {
- // has pres
- if (on_res_op == "and") {
- if (type_conds_.size() == 1) {
- const Bitmap* temp_p =
- field_group_set_ptr->get_bitmap(fields_[0], type_conds_[0]);
- if (temp_p) {
- pres->Exclude(temp_p);
- }
+ return calc_self_bitmap(field_group_set_ptr);
+ }
- } else if (type_conds_.size() > 1) {
- BitmapPtr temp =
- field_group_set_ptr->make_field_copy(fields_[0], type_conds_);
- if (temp) {
- pres->Exclude(temp.get());
- }
- }
- } else if (on_res_op == "or") {
- BitmapPtr temp = calc_self_bitmap(field_group_set_ptr);
- if (temp) {
- // or 计算一个空条件,返回原结果
- pres->Union(temp.get());
- }
- } else {
+ if (on_res_op == "and") {
+ BitmapPtr temp = calc_self_bitmap(field_group_set_ptr);
+ if (!temp) {
return nullptr;
}
+ pres->Intersect(temp.get());
+ } else if (on_res_op == "or") {
+ BitmapPtr temp = calc_self_bitmap(field_group_set_ptr);
+ if (temp) {
+ pres->Union(temp.get());
+ }
+ } else {
+ return nullptr;
}
+
return pres;
}
diff --git a/tests/vectordb/test_filter_ops.py b/tests/vectordb/test_filter_ops.py
index a1b50ad56..608d64520 100644
--- a/tests/vectordb/test_filter_ops.py
+++ b/tests/vectordb/test_filter_ops.py
@@ -714,6 +714,22 @@ class TestFilterOpsPath(unittest.TestCase):
self._search({"op": "must_not", "field": "file_path", "conds": ["/a"]}), [4, 5]
)
+ def test_path_must_not_after_another_condition(self):
+ """Test MustNot excluding path prefix when it is not the first condition of an and"""
+ # OpenViking always puts the tenant filter first and a caller's filter after it.
+ every_record = {"op": "must", "field": "id", "conds": [1, 2, 3, 4, 5]}
+
+ def after_every_record(condition):
+ return {"op": "and", "conds": [every_record, condition]}
+
+ # MustNot /a/b -> exclude 1, 2 -> remaining 3, 4, 5
+ must_not_ab = {"op": "must_not", "field": "file_path", "conds": ["/a/b"]}
+ self.assertEqual(self._search(after_every_record(must_not_ab)), [3, 4, 5])
+ # MustNot /a with depth=1 -> exclude 3 -> remaining 1, 2, 4, 5
+ must_not_a_d1 = {"op": "must_not", "field": "file_path", "conds": ["/a"], "para": "-d=1"}
+ self.assertEqual(self._search(must_not_a_d1), [1, 2, 4, 5])
+ self.assertEqual(self._search(after_every_record(must_not_a_d1)), [1, 2, 4, 5])
+
def test_path_must_normalize_leading_slash(self):
"""Test Must/MustNot when path values are missing leading '/'"""
data = [