I have a simple parent/child graph in neo4j. I would like search the parents (and its children) with a property value in parent node or in child node. A parent can have (or not) a list of children
I tried this query
MATCH (p:Parent)
WHERE p.lastName =~ "(?i).*.*"
RETURN p AS parent, [] as children
UNION
MATCH (p:Parent) - [:REL] -> (c:Child)
WHERE p.lastName =~ "(?i).*1234.*" OR c.name =~ "(?i).*.*"
RETURN p AS parent, c AS collect(children)
but it's not perfect: this query return not distinct result.
Do you know a better query to have a list of parents and its children with property value in parent node or child node.
The logic should be:
if (p.lastName == x) OR (p.children.name == x)
return p
UPDATE
I give you more elements:
Example:
(p1:Parent { lastName: "AAA" }) no children
(p2:Parent { lastName: "BBB" }) 2 children
(c1:Child { name: "CCC" })
(c2:Child { name: "DDD"})
If I execute this query
MATCH (p:Parent)
OPTIONAL MATCH (p)-[:REL]->(c:Child)
WHERE p.lastName CONTAINS 'DDD' OR c.name CONTAINS 'DDD'
RETURN p AS parent, collect(c) AS children;
the result is p1, p2 with c2. I would like only p2 with c2