在 Spring Data Neo4j 中运行 APOC subGraphAll()

问题描述 投票:0回答:1

我正在使用 Spring Data Neo4j,并尝试执行以下代码:

    @Query("""
         MATCH (orders:Order {id:1})
         CALL apoc.path.subgraphAll(orders, {})
         YIELD nodes, relationships
         RETURN  nodes, relationships
        """)
List<Order> getOrders(@Param("orderId") Long orderId);

但是,它仅返回一个具有空字段的订单,而不是两个已填充所有字段的订单。

之前,我使用以下代码:

      @Query("""
         MATCH path = (orders:Order {id: $orderId})-[*]-(commons)
         RETURN path
        """)
List<Order> getOrders(@Param("orderId") Long orderId);

但是,添加 10-20 个节点后,它会变得很长......所以我要么需要改进这个查询,要么以某种方式让 Spring 与 APOC 程序一起工作。

如果您能在这件事上指导我,我真的很感激。

提前谢谢您!

neo4j cypher spring-data-neo4j
1个回答
0
投票

由于您的原始查询返回路径,您可以尝试使用 apoc.path.expand 而不是

apoc.path.subgraphAll
:

@Query("""
   MATCH (ord:Order {id: $orderId})
   CALL apoc.path.expand(ord, null, null, 0, -1) YIELD path
   RETURN path
""")
List<Order> getOrders(@Param("orderId") Long orderId);

此外,如果可以接受,解决方法是限制可变长度路径搜索的长度。例如,要将搜索限制为最多 10 跳,您可以在原始查询中将

[*]
替换为
[*..10]
。或者在我的查询中使用
10
而不是
-1

© www.soinside.com 2019 - 2024. All rights reserved.