Return Nodes related with a relatiohip to other Neo4j

时间:2015-05-24 20:23:34

标签: neo4j nodes

i have just started using Neo4j, and after creating the whole graph i'm trying to get all the nodes related to another one by a relatioship.

Match (n)-[Friendship_with]->({Name:"Gabriel"}) return n

That should give me the nodes that are friend of Gabriel, what i'm doing wrong? I have used too this:

 Match n-[r:Friendship_with]->n1 where n1.Name="Gabriel" return n

That give me some nodes, but some of then aren't directly related to Gabriel (for example, Maria is friend of Gabriel, she appears when i write that, but Alex who is friend of Maria and not from Gabriel, appear too)

2 个答案:

答案 0 :(得分:2)

This is weird. Your query is correct.

I would suggest to check your data. Are you sure there isn't any direct connection between Alex and Gabriel ?

You could visualize your graph and see what is happening exactly in the neo4j browser. Just type a query with a bit more info like:

MATCH (n)-[f:Friendship_with]->(p {Name:"Gabriel"}) return n,f,p

and use the graph view to inspect your data.

EDIT:

As Pointed out by Michael, your first query is missing a colon in front of the specified relationship label "Friendship_with". So neo4j thinks it is a (rather long) variable name for your relationships, just as 'n' or 'n1'. It will thus retrieve anything that is connected to Gabriel without filtering by relationship label.

It doesn't explain though why you:

  1. get the same results with the first and second query
  2. get a 2nd degree relation as a result

so check your data anyway :)

答案 1 :(得分:2)

  1. You forgot the colon before :Friendship_with
  2. Don't forget to provide labels, e.g. (n1:Person {Name:"Gabriel"})
  3. Also some of your friendships might go in the other direction, so leave off the direction-arrow: Match (n:Person)-[Friendship_with]-(:Person {Name:"Gabriel"}) return n