You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Neo4j中查找指定k度关联的客户节点并返回路径?

指定k度Customer关联节点查询方案(Neo4j + Python)

核心Cypher查询(基于APOC插件)

利用Neo4j的APOC扩展库,通过定义固定遍历序列的方式,实现指定k度的路径查询,无需手动拼接模式字符串。

MATCH (start:Customer {id: $start_id})
CALL apoc.path.expandConfig(start, {
  // 严格匹配Customer→Transaction→Terminal←Transaction←Customer的遍历序列
  sequence: [
    {type: "MADE", direction: "OUTGOING"},
    {type: "AT", direction: "OUTGOING"},
    {type: "AT", direction: "INCOMING"},
    {type: "MADE", direction: "INCOMING"}
  ],
  iterations: $k,  // 指定遍历次数,对应k度关联
  endNodeFilter: ">Customer",  // 仅返回最终到达的Customer节点
  filterStartNode: false,
  returnPaths: true  // 返回完整路径用于验证
}) YIELD path
WHERE start <> endNode(path)  // 排除起始节点自关联
RETURN path, endNode(path) AS target_customer

关键参数说明

  • sequence:定义每一段的遍历规则,严格遵循业务路径模式
  • iterations: $k:控制关联度数,例如k=3会遍历3次完整序列,得到经过3次终端中转的关联客户
  • returnPaths: true:确保返回完整的路径信息,方便手动验证节点和关系的正确性

注意:需要预先安装Neo4j APOC插件(Neo4j Desktop可直接在插件市场安装,服务器版需下载对应版本的jar包放入plugins目录并重启)

原生Cypher递归方案(无APOC依赖)

如果无法使用APOC,可采用递归查询实现,但性能略低于APOC方案:

MATCH (start:Customer {id: $start_id})
WITH start, [] AS path, 0 AS current_k
CALL {
  WITH start, path, current_k
  // 正向遍历
  MATCH p = (start)-[:MADE]->(:Transaction)-[:AT]->(:Terminal)<-[:AT]-(:Transaction)<-[:MADE]-(next_cust:Customer)
  WHERE next_cust <> start
  RETURN next_cust, path + nodes(p) AS new_path, current_k + 1 AS new_k
  UNION ALL
  WITH start, path, current_k
  // 反向遍历(如果需要双向查找)
  MATCH p = (prev_cust:Customer)-[:MADE]->(:Transaction)-[:AT]->(:Terminal)<-[:AT]-(:Transaction)<-[:MADE]-(start)
  WHERE prev_cust <> start
  RETURN prev_cust, path + reverse(nodes(p)) AS new_path, current_k + 1 AS new_k
}
WITH next_cust, new_path, new_k
WHERE new_k = $k
RETURN new_path AS path, next_cust AS target_customer

Python执行示例

使用官方Neo4j Python驱动执行上述查询,处理并打印结果:

1. 安装依赖

pip install neo4j

2. 代码实现

from neo4j import GraphDatabase

def query_k_degree_customers(uri, auth, start_customer_id, k):
    # 初始化驱动
    driver = GraphDatabase.driver(uri, auth=auth)
    with driver.session() as session:
        # 执行Cypher查询
        result = session.run(
            """
            MATCH (start:Customer {id: $start_id})
            CALL apoc.path.expandConfig(start, {
              sequence: [
                {type: "MADE", direction: "OUTGOING"},
                {type: "AT", direction: "OUTGOING"},
                {type: "AT", direction: "INCOMING"},
                {type: "MADE", direction: "INCOMING"}
              ],
              iterations: $k,
              endNodeFilter: ">Customer",
              filterStartNode: false,
              returnPaths: true
            }) YIELD path
            WHERE start <> endNode(path)
            RETURN path, endNode(path) AS target_customer
            """,
            start_id=start_customer_id,
            k=k
        )
        # 遍历结果并输出验证
        for idx, record in enumerate(result, 1):
            path = record["path"]
            target_cust = record["target_customer"]
            print(f"--- 结果 {idx} ---")
            print(f"目标客户ID: {target_cust['id']}")
            print("路径节点详情:")
            for node in path.nodes:
                node_label = list(node.labels)[0]
                print(f"  [{node_label}] ID: {node['id']}")
    driver.close()

# 调用示例
if __name__ == "__main__":
    NEO4J_URI = "bolt://localhost:7687"
    NEO4J_AUTH = ("neo4j", "your_database_password")
    query_k_degree_customers(
        uri=NEO4J_URI,
        auth=NEO4J_AUTH,
        start_customer_id=1001,  # 替换为你的起始客户ID
        k=3  # 指定关联度数
    )

代码说明

  • 使用参数化查询避免注入风险
  • 输出完整路径的节点类型和ID,方便手动验证路径正确性
  • 兼容Neo4j 4.x及以上版本

内容的提问来源于stack exchange,提问作者Holyamirali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 04:02:13