user157629
user157629

Reputation: 634

What is missing in this Cypher query?

I'm learning Cypher and I created a 'Crime investigation' project on Neo4j.

I'm trying to return as an output the parent that only has two sons/daughters in total and each member of the family must have committed a crime. So, in order to get this in the graph, I executed this query:

match(:Crime)<-[:PARTY_TO]-(p:Person)-[:FAMILY_REL]->(s:Person)-[:PARTY_TO]->(:Crime)
where size((p)-[:FAMILY_REL]->())=2
return p, s

FAMILY_REL relation shows the sons the Person (p) and PARTY_TO relation shows the Crime nodes a Person have committed.

The previous query it's not working as it should. It shows parents with more than two sons and also sons that have just one son.

What is wrong with the logic of the query?

Upvotes: 0

Views: 66

Answers (1)

cybersam
cybersam

Reputation: 66989

SIZE((p)-[:FAMILY_REL]->()) counts all children of p, including ones who had committed no crimes.

This query should work better, as it only counts children who are criminals:

MATCH (:Crime)<-[:PARTY_TO]-(p:Person)-[:FAMILY_REL]->(s:Person)-[:PARTY_TO]->(:Crime)
WITH p, COLLECT(s) AS badKids
WHERE SIZE(badKids) = 2
RETURN p, badKids

Upvotes: 1

Related Questions