1
votes

my relationship graph as below pic
Using MATCH (n:Person {name:'1'})-[]-()-[r]-(m) RETURN m,r will return "4" and "5",but cannot get the relationship between "4" and "5",actually,"4" follow "5","r" just represent the friends of 4(4-->2) and 5(5-->3).
In spring data neo4j
domain.java

@NodeEntity(label = "Person")
public class PersonTest {
@Id
private Long id;
@Property(name = "name")
private String name;
@Relationship(type = "Follow")  //direction=Relationship.DIRECTION
private List<PersonTest> friends;

Repository.java

public interface PersonRepository extends Neo4jRepository<PersonTest,Long> {
    @Query("MATCH (n:Person {name:{name}})-[]-()-[r]-(m) RETURN m,r")
    Collection<PersonTest> graph(@Param("name") String name);
}

Service.java

 Collection<PersonTest> persons = personRepository.graph(name);
 Iterator<PersonTest> result = persons.iterator();
 while (result.hasNext()) {
      for (PersonTest friend : p.getFriends()) {
           //........here will get 2 and 3!
      }
 }

How to resolve this problem??get the relationship between "4" and "5".

1

1 Answers

3
votes

To find related children at a certain level, you can use a two-sided pattern of variable length:

MATCH (n:Person {name:'1'})-[:Follow*2]->(m)-[r:Follow]-()<-[:Follow*2]-(n) 
RETURN m,r

http://console.neo4j.org/r/el2x80

Update. I think that this is a more correct query:

MATCH (n:Person {name:'1'})-[:Follow*2]->(m:Person)-[r:Follow]-(k:Person) 
WHERE (n)-[:Follow*2]->(k) 
RETURN m, r

http://console.neo4j.org/r/bv2u8k