feat: support metadata condition filter string array (#23111)

Signed-off-by: kenwoodjw <blackxin55+@gmail.com>
This commit is contained in:
kenwoodjw
2025-07-30 16:13:45 +08:00
committed by GitHub
parent 11ec62ca70
commit 28478cdc41
6 changed files with 32 additions and 2 deletions

View File

@@ -148,6 +148,8 @@ SupportedComparisonOperator = Literal[
"is not", "is not",
"empty", "empty",
"not empty", "not empty",
"in",
"not in",
# for number # for number
"=", "=",
"", "",

View File

@@ -13,6 +13,8 @@ SupportedComparisonOperator = Literal[
"is not", "is not",
"empty", "empty",
"not empty", "not empty",
"in",
"not in",
# for number # for number
"=", "=",
"", "",

View File

@@ -74,6 +74,8 @@ SupportedComparisonOperator = Literal[
"is not", "is not",
"empty", "empty",
"not empty", "not empty",
"in",
"not in",
# for number # for number
"=", "=",
"", "",

View File

@@ -602,6 +602,28 @@ class KnowledgeRetrievalNode(BaseNode):
**{key: metadata_name, key_value: f"%{value}"} **{key: metadata_name, key_value: f"%{value}"}
) )
) )
case "in":
if isinstance(value, str):
escaped_values = [v.strip().replace("'", "''") for v in str(value).split(",")]
escaped_value_str = ",".join(escaped_values)
else:
escaped_value_str = str(value)
filters.append(
(text(f"documents.doc_metadata ->> :{key} = any(string_to_array(:{key_value},','))")).params(
**{key: metadata_name, key_value: escaped_value_str}
)
)
case "not in":
if isinstance(value, str):
escaped_values = [v.strip().replace("'", "''") for v in str(value).split(",")]
escaped_value_str = ",".join(escaped_values)
else:
escaped_value_str = str(value)
filters.append(
(text(f"documents.doc_metadata ->> :{key} != all(string_to_array(:{key_value},','))")).params(
**{key: metadata_name, key_value: escaped_value_str}
)
)
case "=" | "is": case "=" | "is":
if isinstance(value, str): if isinstance(value, str):
filters.append(Document.doc_metadata[metadata_name] == f'"{value}"') filters.append(Document.doc_metadata[metadata_name] == f'"{value}"')

View File

@@ -32,6 +32,8 @@ export const getOperators = (type?: MetadataFilteringVariableType) => {
ComparisonOperator.endWith, ComparisonOperator.endWith,
ComparisonOperator.empty, ComparisonOperator.empty,
ComparisonOperator.notEmpty, ComparisonOperator.notEmpty,
ComparisonOperator.in,
ComparisonOperator.notIn,
] ]
case MetadataFilteringVariableType.number: case MetadataFilteringVariableType.number:
return [ return [

View File

@@ -591,8 +591,8 @@ const translation = {
'not empty': '不为空', 'not empty': '不为空',
'null': '空', 'null': '空',
'not null': '不为空', 'not null': '不为空',
'in': '', 'in': '',
'not in': '不', 'not in': '不',
'all of': '全部是', 'all of': '全部是',
'exists': '存在', 'exists': '存在',
'not exists': '不存在', 'not exists': '不存在',