如何查询Neo4j节点以及节点之间的关系?

How to query Neo4j nodes and the relationship between the nodes?

现在我有这样的图表:

然后我想查询"SGJ""HAVE"

的节点
MATCH (n:User) -[R:MASTER]-> (k:KNode)
WHERE n.username={username}
RETURN k

但我得到的结果是这样的:

{
    "id": 360,
    "children": null,
    "name": "Arrays",
    "intro": "this is an intro"
},
{
    "id": 300,
    "children": null,
    "name": "Java",
    "intro": "this is an intro"
}

这些节点之间的关系刚刚消失,我希望我可以查询关系仍然存在的节点:

{
    "id": 360,
    "children": [
         {
            "id": 300,
            "children": null,
            "name": "Java",
            "intro": "this is an intro"
         }
     ],
    "name": "Arrays",
    "intro": "this is an intro"
}

这是实体定义:

@Data
@NodeEntity
public class KNode {

    @GraphId
    Long id;
    @Relationship(type = "BELONGS_TO", direction = Relationship.INCOMING)
    List<KNode> children;
    private String name;
    private String intro;

}

有什么解决办法吗?谢谢。

您只是return正在创建一个节点而不是路径。尝试向 return 以及 children 和关系 :

发送这些请求之一
MATCH (n:User) -[R:MASTER]-> (k:KNode)
WHERE n.username={username}
OPIONAL MATCH p=(k)-[r]-(c)
RETURN p

或:

MATCH (n:User) -[R:MASTER]-> (k:KNode)
WHERE n.username={username}
WITH k
MATCH p=(k)-[r]-(c)
RETURN p