4

I want to Retrieve all nodes and relationship connected to a 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 Following Response :

[ {
  "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 if i want to see the relationship of this node again i will have to hit the link "http://localhost:7474/db/data/node/82/relationships/all"

Cant we get Data in which Node and its relationship are shown directly instead of link to relationship without hitting the link again????

2nd thing I have tried to do is to get this from cypher query :

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

But this also didn't work because at (b) and (c) there will be multiple values as a result for which i will have to iterate and write another query

Cant we get this done in single query because i have so many connected relationship that it is getting hard to iterate again and again. Any Help would be Appreaciated.

Shiv
  • 4,569
  • 4
  • 25
  • 39

3 Answers3

5

It's easy to get all nodes connected to a given node with Cypher

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

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

If you know the bounds of the connections, specify it explicitly in the query would be helpful for performance,

START a=node(3)
MATCH (a)-[:KNOWS*1..3]->(d)
RETURN Distinct d
Lisa Li
  • 2,562
  • 15
  • 11
  • ya you are right i can do this way but what if (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d) and we get multiple nodes for (b) and (C) ?? we will have to write separate query for each cant we do it in a single query ?? – Shiv Sep 12 '13 at 04:56
  • I am not sure I understand your concern. "Match (a)-[:KNOWs*1..3]-b" would return all the nodes that have 1 or 2 or 3 connections with a. For example, if you have a->b1, a->b2 and a->b3, "Match a-[:KNOWS]-b" would match all of three paths, and return b1, b2 and b3. right? – Lisa Li Sep 12 '13 at 12:51
0

Regarding the question about multiple nodes, or duplicate nodes. I understand what you mean. Here is something I did with such a query to weed out duplicates. More about if a KNOWS b which KNOWS c, but c is really a. Kind of like that. 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
Mark
  • 1
-1

If you make your query

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

The r(x) will return the respective details regarding the relationship. There will be a "row" for each path that matches the query.

If you define your deserializer so it recognizes the r(x) and constructs a relationship rather than an entity then you should be able to do it all in one query.

rogermushroom
  • 5,486
  • 4
  • 42
  • 68