简体   繁体   中英

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

Now I have a graph like this:

样本图

Then I want to query the nodes which the "SGJ" "HAVE"

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

But I get the result like this:

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

The relationship between these nodes just gone, I hope I can query the nodes with the relationship remain like:

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

Here's the entity definition:

@Data
@NodeEntity
public class KNode {

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

}

Is there any solution? Thanks.

You are just returning a node instead of path. Try one of these requests to return also the children and relationship :

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

Or :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM