Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching not exists in Neo4j via Cypher

I have some relations between persons in my graph.

my data (generate script below)

create (s:Person {name: "SUE"}) 
create(d:Person {name: "DAVID"}) 
create(j:Person {name: "JACK"}) 
create(m:Person {name: "MARY"}) 
create(js:Person {name: "JASON"}) 
create(b:Person {name: "BOB"}) 
create(a1:Adress {id:1}) 
create(a2:Adress {id:2}) 
create(a3:Adress {id:3}) 
create(a4:Adress {id:4}) 
create(a5:Adress {id:5}) 
merge (d)-[:MOTHER]->(s) 
merge(j)-[:MOTHER]->(s) 
merge(js)-[:MOTHER]->(m) 
merge(b)-[:MOTHER]->(m) 
merge(b)-[:CURRENT_ADRESS]->(a1) 
merge(js)-[:CURRENT_ADRESS]->(a2) 
merge(j)-[:CURRENT_ADRESS]->(a3) 
merge(s)-[:CURRENT_ADRESS]->(a4) 
merge(d)-[:CURRENT_ADRESS]->(a5)

enter image description here;

I can get mothers who live with her child:

MATCH (p:Person)-[:CURRENT_ADRESS]->(a:Adress)<-[:CURRENT_ADRESS]-(t), (t)-[:MOTHER]->(p)
return p.name,t.name

p.name  t.name
MARY    JASON

but i want to get mothers who is not living with any child of her.

How can i do that in Cyper?

like image 912
AgonyClanKios Avatar asked Mar 15 '16 10:03

AgonyClanKios


People also ask

Is exist in Neo4j?

exists() The function exists() returns true if a match for the given pattern exists in the graph, or if the specified property exists in the node, relationship or map. null is returned if the input argument is null .

Is null in Cypher?

In Cypher, null is used to represent missing or undefined values. Conceptually, null means a missing unknown value and it is treated somewhat differently from other values. For example getting a property from a node that does not have said property produces null .

Which clause is used to search the data with a specified pattern in Neo4j?

The MATCH clause allows you to specify the patterns Neo4j will search for in the database.


1 Answers

Actually in your graph, everybody is living at a different address due to different identifiers.

Let's build a graph example introducing the sister which lives at the same address :

CREATE 
(p:Person)-[:MOTHER]->(m:Person),
(p)-[:FATHER]->(f:Person),
(p)-[:SISTER]->(s:Person),
(p)-[:CURRENT_ADDRESS]->(a:Adress),
(m)-[:CURRENT_ADDRESS]->(b:Adress),
(f)-[:CURRENT_ADDRESS]->(c:Adress),
(s)-[:CURRENT_ADDRESS]->(a)

enter image description here

Now this is very simple, match family members that don't have a CURRENT_ADDRESS relationship in depth2 to the family member :

MATCH (p:Person)-[:MOTHER|:FATHER|:SISTER]->(familyMember)
WHERE NOT EXISTS((p)-[:CURRENT_ADDRESS*2]-(familyMember))
RETURN familyMember

enter image description here

like image 178
Christophe Willemsen Avatar answered Sep 28 '22 00:09

Christophe Willemsen