Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undirected Relationship in Neo4J

I am using Spring Data Neo4J to define a undirected relationship between different persons. A sample entity class has been provided below.

@NodeEntity(label="Person")
public class Person {

    @GraphId
    private Long id;
    private String name;

    @Relationship(type = "FRIEND_WITH", direction=Relationship.UNDIRECTED)
    List<Person> friends;
}

As above, a Person has a list of friends of type "Person". The relationship is kept undirected to ensure that if a person A is "Friend_With" person B, then person B is also "Friend_With" Person A.

The code to add a friend is provided below

    if((person.getFriends() == null)||(person.getFriends().size()==0)){
        List<Person> friendList = new ArrayList<Person>();
        friendList.add(friend);
        person.setFriends(friendList);
    }else{
        person.getFriends().add(friend);
    }

    personRepository.save(person);

I have added PersonB as a friend of PersonA,so ideally, this should mean

PersonA - [:FRIEND_WITH] -> PersonB
PersonB - [:FRIEND_WITH] -> PersonA

as the relationship is undirected

But when I am querying, in Neo4J with

MATCH (p:Person)-[r:FRIEND_WITH]->(b:Person) where p.name = "PersonA" return p,b

I am getting the result as PersonA, PersonB. But when I am querying

MATCH (p:Person)-[r:FRIEND_WITH]->(b:Person) where p.name = "PersonB" 

no rows are returned. Thus the direction specified in the entity class does not seem to work. Also the Graph in Neo4J browser shows a directed edge from PersonA to PersonB.

All I want is that if PersonA is a friend of PersonB, the I will get the results, no matter, which way I ask. The code seems to work for

MATCH (p:Person)-[r:FRIEND_WITH]-(b:Person) where p.name = "PersonB" 

where the "->" is replaced by "-", but I do not want to use this.

What should I do ?

I am using spring-data-neo4j.version 4.0.0.RELEASE and spring-boot version spring-boot-starter-parent 1.3.0.M5

like image 333
Soumya Avatar asked Oct 27 '15 12:10

Soumya


People also ask

Does Neo4j allow undirected relationships?

In Neo4j, all relationships have a direction. However, you can have the notion of undirected relationships at query time.

What are relationships in Neo4j?

Relationships describes a connection between a source node and a target node. Relationships always has a direction (one direction).

How are relationships stored in Neo4j?

Properties are stored as a linked list of property records, each holding a key and value and pointing to the next property. Each node and relationship references its first property record. The Nodes also reference the first relationship in its relationship chain. Each Relationship references its start and end node.

How do you add a relationship type in Neo4j?

Creating Relationships We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.


1 Answers

In Neo4j, ALL relationships are directed.

However, you can have the notion of undirected edges at query time. Just remove the direction from your MATCH query :

MATCH (p:Person)-[r:FRIEND_WITH]-(b:Person) where p.name = "PersonB" 

Why don't you want to use this ?

like image 79
Christophe Willemsen Avatar answered Sep 21 '22 11:09

Christophe Willemsen