1
votes

I am using Neo4j (version 3.4.1) and Spring-data-neo4j (5.0.10.RELEASE) in my application. I am also using OGM.

I have the below relationship between my nodes:

enter image description here

I want to find all the nodes in each path from node A to node F (in the order of traversal)

For e.g : For path A-->B-->F I want nodes A,B,F (in that order)

For path A-->C-->D-->F I want nodes A,C,D,F (in that order) etc.

I want to capture the nodes traversed in each path separately.

The cypher query that I came up with is as below:

@Query("MATCH p=((u:User{name:{nameOne}})-[:RELATED*1..3]-(v:User{name:{nameTwo}})) RETURN p")
List<User> getRelationBetweenUsers(@Param("nameOne") String nameOne, @Param("nameTwo") String nameTwo);

I also tried using List<List<User>> as return type so as to get nodes on each path as separate list as below

 @Query("MATCH p=((u:User{name:{nameOne}})-[:RELATED*1..3]-(v:User{name:{nameTwo}})) RETURN p")
    List<List<User>> getRelationBetweenUsers(@Param("nameOne") String nameOne, @Param("nameTwo") String nameTwo);

However this returns all the nodes traversed in multiple paths together whereas I want them per path.

 i.e result is List <A,B,C,D,F> where as what I want is
List<List<A,B,F>, List<A,C,D,F>, List<A,D,F>>.

Hope you get the idea.

Can anybody give some pointers/suggestions on how I can achieve the expected results.

Regards, Varun

1
A,B,C,D,F is not a valid path, given the relationships in your illustration. Also, the result is a list of lists (of nodes), but you say that you are only getting back a list of nodes. Are you processing the result properly?cybersam
Thanks @cybersam. Apologies I had got the diagram wrong as I had handcrafted it. I have corrected the diagram. Also I had tried returning both List<Nodes> and List<List<Nodes>>, I was returned the same result from the query. I have updated the question to include this informationLearnToLive
Your new diagram is even more confusing, as I do not know how it is supposed to produce your stated desired results. Do you want to get paths where all relationships are in a consistent forward direction? If so, try changing your MATCH query so that the relationship pattern contains an arrow to specify the direction (e.g,, -[:RELATED*1..3]->).cybersam
Sorry for the delayed response @Cybersam. I wanted to get nodes on all the paths matching the given relationships which were 1 to 3 hop away. Also the nodes should be in order of hop.LearnToLive

1 Answers

0
votes

I was able to find an answer to my own question. I took hint from another SO answer. Spring Data | Neo4J | Querying for the path in the correct order

This is what I did to get the desired result

@Query("MATCH p=((u:User{name:{nameOne}})-[:RELATED*1..3]-(v:User{name:{nameTwo}})) RETURN nodes(p) as users")
org.neo4j.ogm.model.Result getRelationBetweenUsers(@Param("nameOne") String nameOne, @Param("nameTwo") String nameTwo);

Within my service class where I call the above method, I have the below implementation :

List<List<User>> usersOnDifferentPaths = new ArrayList<>();
Iterable<Map<String, Object>> queryResults = userRepository.getRelationBetweenUsers(nameOne, nameTwo).queryResults();
queryResults.forEach(queryResult -> {
            List<User> usersOnSinglePath = (List<User>) queryResult.get("users");
            usersOnDifferentPaths.add(usersOnSinglePath);
        });

Hope someone finds it helpful.

PS: Note I am deliberately skipping the relationships in the query as I am only interested in getting nodes.

Regards,

V