Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(metadata): preserve string arrays in in-memory filters (#21065)

adwsf9233c809cd1
diff --git a/internal/service/metadata_filter.go b/internal/service/metadata_filter.go
index db24bc538a..6d6540bbef 100644
--- a/internal/service/metadata_filter.go
+++ b/internal/service/metadata_filter.go
@@ -234,7 +234,7 @@ func ApplyMetaFilter(metaData common.MetaData, filters []MetaFilterCondition, lo
}
// convertToMetaCondition converts a MetaFilterCondition to common.MetaCondition,
-// normalizing operator symbols and value types for compatibility with common.MetaFilter.
+// normalizing operator symbols and value types for common.MetaFilter.
//
// Operator normalization:
// - "==" = "=" "!=" = "≠"
@@ -243,8 +243,10 @@ func ApplyMetaFilter(metaData common.MetaData, filters []MetaFilterCondition, lo
// (see common.metadata_utils.operatorMapping for the full list)
//
// Value conversion:
-// - "in" / "not in": comma-separated string → []interface{} (as expected by common.MetaFilter)
-// - all other operators: passed through as-is (string)
+// - "in" / "not in": comma-separated strings become []interface{};
+// arrays are preserved only when every member is a string. Arrays with
+// non-string members become nil so the condition matches no documents.
+// - all other operators: passed through as-is.
func convertToMetaCondition(f MetaFilterCondition) common.MetaCondition {
mc := common.MetaCondition{
Key: f.Key,
@@ -253,7 +255,19 @@ func convertToMetaCondition(f MetaFilterCondition) common.MetaCondition {
}
switch f.Op {
case "in", "not in":
- strVal, _ := f.Value.(string)
+ if values, ok := f.Value.([]interface{}); ok {
+ for _, value := range values {
+ if _, ok := value.(string); !ok {
+ mc.Value = nil
+ return mc
+ }
+ }
+ return mc
+ }
+ strVal, ok := f.Value.(string)
+ if !ok {
+ return mc
+ }
parts := strings.Split(strVal, ",")
arr := make([]interface{}, 0, len(parts))
for _, p := range parts {
diff --git a/internal/service/metadata_filter_test.go b/internal/service/metadata_filter_test.go
index 732ba3026d..626bcb7e4d 100644
--- a/internal/service/metadata_filter_test.go
+++ b/internal/service/metadata_filter_test.go
@@ -17,6 +17,8 @@
package service
import (
+ "encoding/json"
+ "slices"
"sort"
"strings"
"testing"
@@ -109,6 +111,48 @@ func TestApplyMetaFilter_NotIn(t *testing.T) {
}
}
+func TestApplyMetaFilterSetValues(t *testing.T) {
+ metas := common.MetaData{
+ "category": {"A": {"doc1"}, "B": {"doc2"}, "C": {"doc3"}, "A,B": {"doc4"}},
+ }
+ tests := []struct {
+ name string
+ op string
+ value any
+ want []string
+ }{
+ {"json_in", "in", []any{"A", "B"}, []string{"doc1", "doc2"}},
+ {"json_not_in", "not in", []any{"A", "B"}, []string{"doc3", "doc4"}},
+ {"literal_comma", "in", []any{"A,B"}, []string{"doc4"}},
+ {"invalid_not_in", "not in", float64(123), nil},
+ {"null_not_in", "not in", []any{"A", nil}, nil},
+ {"object_not_in", "not in", []any{"A", map[string]any{"value": "B"}}, nil},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ filters := []MetaFilterCondition{{Key: "category", Value: tt.value, Op: tt.op}}
+ if got := asSortedIDs(ApplyMetaFilter(metas, filters, "and")); !slices.Equal(got, tt.want) {
+ t.Errorf("ApplyMetaFilter() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestApplyMetaDataFilterManualNotInArray(t *testing.T) {
+ metas := common.MetaData{
+ "category": {"A": {"doc1"}, "B": {"doc2"}, "C": {"doc3"}},
+ }
+ var filter map[string]any
+ if err := json.Unmarshal([]byte(`{"method":"manual","manual":[{"key":"category","op":"not in","value":["A"]}]}`), &filter); err != nil {
+ t.Fatal(err)
+ }
+ // No dataset IDs means the request uses the in-memory filter.
+ got, empty := ApplyMetaDataFilter(t.Context(), filter, metas, "", nil, []string{"doc1", "doc3"}, nil)
+ if !slices.Equal(got, []string{"doc3"}) || empty {
+ t.Errorf("ApplyMetaDataFilter() = (%v, %v), want ([doc3], false)", got, empty)
+ }
+}
+
func TestApplyMetaFilter_StartWith(t *testing.T) {
metas := common.MetaData{
"code": {"ABC-123": {"doc1"}, "XYZ-456": {"doc2"}},