Neo4j: getting all nodes and relationships related to Node in Neo4j Rest OR via Cypher

I want to get all the nodes and relationships associated with node.

I tried to do this in two ways:

1st Through Neo4j REST API I Tried This

URI traverserUri = new URI( startNode.toString() + "/traverse/node" ); WebResource resource = Client.create() .resource( traverserUri ); String jsonTraverserPayload = t.toJson(); ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( jsonTraverserPayload ) .post( ClientResponse.class ); System.out.println( String.format( "POST [%s] to [%s], status code [%d], returned data: " + System.getProperty( "line.separator" ) + "%s", jsonTraverserPayload, traverserUri, response.getStatus(), response.getEntity( String.class ) ) ); response.close(); 

And get the following answer:

 [ { "outgoing_relationships" : "http://localhost:7474/db/data/node/82/relationships/out", "data" : { "band" : "The Clash", "name" : "Joe Strummer" }, "traverse" : "http://localhost:7474/db/data/node/82/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/82/properties/{key}", "all_relationships" : "http://localhost:7474/db/data/node/82/relationships/all", "self" : "http://localhost:7474/db/data/node/82", "properties" : "http://localhost:7474/db/data/node/82/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/82/relationships/in", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/in/{-list|&|types}", "create_relationship" : "http://localhost:7474/db/data/node/82/relationships" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/83/relationships/out", "data" : { }] 

But the problem is that I want to see the relationship of this node again, I have to click on the link "http://localhost:7474/db/data/node/82/relationships/all"

It is not possible to get data in which the node and its link is shown directly instead of the link to the link without hitting the link again.

The second thing I was trying to do was get this from a cypher request:

 START a=node(3) MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d) RETURN a,b,c,d 

But this also did not work, because in (b) and (c) there will be several values ​​as a result, for which I will have to iterate and write another query

It is not possible to do this in a single query because I have so many interrelated relationships that it is becoming increasingly difficult to repeat the iteration. Any help would be fixed.

+4
source share
3 answers

It is easy to get all the nodes associated with a given node with Cypher

 START a=node(3) MATCH (a)-[:KNOWS*]->(d) RETURN distinct d 

But if you have a large number of connected nodes and deep connections, you may not get good performance.

If you know the boundaries of the connections, specify this explicitly in the request, it would be useful for performance,

 START a=node(3) MATCH (a)-[:KNOWS*1..3]->(d) RETURN Distinct d 
+5
source

Regarding the issue of multiple nodes or duplicate nodes. I understand what you mean. Here is what I did with such a request to filter out duplicates. More on whether there is KNOWS b that KNOWS c, but c really. Like this. We can use something like WHERE NOT

 start player=node({0}) match player-[:player.active*2..2]-friendsOfFriends, where not(player-[:player.active]-friendsOfFriends) and player <> friendsOfFriends return distinct friendsOfFriends order by friendsOfFriends.username asc 
0
source

If you make your request

 MATCH (a)-[r1:KNOWS]->(b)-[r2:KNOWS]->(c)-[r3:KNOWS]->(d) RETURN a,r1,b,r2,c,r3,d; 

r(x) will return the relevant data regarding the relation. For each path that matches the query, there will be a "string".

If you define your deserializer to recognize r(x) and build relationships, not an entity, you should be able to do all of this in a single query.

-one
source

Source: https://habr.com/ru/post/1501653/


All Articles