Skip to content

Commit d465cd7

Browse files
committed
add like neo4j filter
1 parent 79ea33c commit d465cd7

File tree

4 files changed

+408
-133
lines changed

4 files changed

+408
-133
lines changed

examples/basic_modules/neo4j_search.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,11 +1091,16 @@ def test_get_by_metadata(graph, filters: list[dict], user_name: str, filter_exam
10911091

10921092
# Example filter for testing - common filter used by multiple tests
10931093
filter_example = {
1094+
# "and": [
1095+
# {"id": "cfe42bd6-ee78-4f6f-b997-8baa0ea957e1"},
1096+
# {"A": "中国广西"},
1097+
# {"created_at": {"gt": "2025-09-19"}},
1098+
# {"created_at": {"lt": "2025-11-26"}},
1099+
# {"tags": {"contains": "mode:fast"}},
1100+
# {"user_name": {"like": "1744"}},
1101+
# ]
10941102
"and": [
1095-
{"id": "cfe42bd6-ee78-4f6f-b997-8baa0ea957e1"},
1096-
{"A": "中国广西"},
1097-
{"created_at": {"gt": "2025-09-19"}},
1098-
{"created_at": {"lt": "2025-11-26"}}
1103+
{"created_at": {"gt": "2025-11-26"}}
10991104
]
11001105
}
11011106
knowledgebase_ids = ["adimin1", "adimin2"]
@@ -1111,5 +1116,5 @@ def test_get_by_metadata(graph, filters: list[dict], user_name: str, filter_exam
11111116
user_name = "memosfeebbc2bd1744d7bb5b5ec57f38e828d"
11121117
scope = "LongTermMemory"
11131118
# test_search_by_embedding(graph, vector, user_name, filter_example,knowledgebase_ids)
1114-
# test_get_all_memory_items(graph, scope, user_name, filter_example, knowledgebase_ids)
1115-
test_get_by_metadata(graph, filters_example, user_name, filter_example, knowledgebase_ids)
1119+
test_get_all_memory_items(graph, scope, user_name, filter_example, knowledgebase_ids)
1120+
# test_get_by_metadata(graph, filters_example, user_name, filter_example, knowledgebase_ids)

examples/basic_modules/polardb_search.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,17 +1272,28 @@ def test_get_by_metadata(graph, filters: list[dict], user_name: str, filter: Opt
12721272
]
12731273
# Example filter for testing - common filter used by multiple tests
12741274
filter_example = {
1275+
# "and": [
1276+
# {"id": "45a4f936-2182-44c6-8c4f-4a9476941e51"},
1277+
# {
1278+
# "A": "中国广西"
1279+
# },
1280+
# {
1281+
# "B": "狗肉"
1282+
# },
1283+
# {"created_at":{"gt":"2025-09-19"}},
1284+
# {"created_at": {"lt": "2025-11-26"}},
1285+
# {"tags": {"contains": "test:zdy"}},
1286+
# {"user_name": {"like": "828"}},
1287+
# {"memory_type": {"like": "WorkingMemory"}},
1288+
# ]
12751289
"and": [
12761290
{"id": "45a4f936-2182-44c6-8c4f-4a9476941e51"},
1277-
{
1278-
"A": "中国广西"
1279-
},
1280-
{
1281-
"B": "狗肉"
1282-
},
1283-
{"created_at":{"gt":"2025-09-19"}},
1291+
{"A": "中国广西"},
1292+
{"created_at": {"gt": "2025-09-19"}},
12841293
{"created_at": {"lt": "2025-11-26"}},
1285-
# {"tags": {"=": "mode:fast"}}
1294+
{"tags": {"contains": "test:zdy"}},
1295+
{"user_name": {"like": "828"}},
1296+
{"memory_type": {"like": "WorkingMemory"}},
12861297
]
12871298
}
12881299

@@ -1298,8 +1309,9 @@ def test_get_by_metadata(graph, filters: list[dict], user_name: str, filter: Opt
12981309
knowledgebase_ids = ["memosfeebbc2bd1744d7bb5b5ec57f38e828d","adimin2"]
12991310

13001311
# Run all tests - uncomment the test you want to run
1301-
test_search_by_embedding(graph, vector, user_name, filter_example,knowledgebase_ids)
1312+
# test_search_by_embedding(graph, vector, user_name, filter_example,knowledgebase_ids)
13021313
# test_get_all_memory_items(graph, "WorkingMemory", False, user_name, filter_example,knowledgebase_ids)
1314+
test_get_by_metadata(graph, filters_example, user_name, filter_example,knowledgebase_ids)
13031315
# test_get_by_metadata(graph, filters_example, user_name, filter_example,knowledgebase_ids)
13041316

13051317
# Or run all tests

src/memos/graph_dbs/neo4j.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,25 @@ def build_filter_condition(condition_dict: dict, param_counter: list) -> tuple[s
14151415
condition_parts.append(f"{node_alias}.{key} {cypher_op} datetime(${param_name})")
14161416
else:
14171417
condition_parts.append(f"{node_alias}.{key} {cypher_op} ${param_name}")
1418+
elif op == "contains":
1419+
# Handle contains operator (for array fields like tags, sources)
1420+
param_name = f"filter_{key}_{op}_{param_counter[0]}"
1421+
param_counter[0] += 1
1422+
params[param_name] = op_value
1423+
1424+
# For array fields, check if element is in array
1425+
if key in ("tags", "sources"):
1426+
condition_parts.append(f"${param_name} IN {node_alias}.{key}")
1427+
else:
1428+
# For non-array fields, contains might not be applicable, but we'll treat it as IN for consistency
1429+
condition_parts.append(f"${param_name} IN {node_alias}.{key}")
1430+
elif op == "like":
1431+
# Handle like operator (for fuzzy matching, similar to SQL LIKE '%value%')
1432+
# Neo4j uses CONTAINS for string matching
1433+
param_name = f"filter_{key}_{op}_{param_counter[0]}"
1434+
param_counter[0] += 1
1435+
params[param_name] = op_value
1436+
condition_parts.append(f"{node_alias}.{key} CONTAINS ${param_name}")
14181437
else:
14191438
# All fields are stored as flat properties in Neo4j (simple equality)
14201439
param_name = f"filter_{key}_{param_counter[0]}"

0 commit comments

Comments
 (0)