Help with query to get all distinct nodes with filtering on specific propertymwithout leaving out some nodes

I have the following graph:

I want to get all distinct node names in that graph. But I want to filter on a property of C. How can I filter on a property of C without leaving out nodes D? Something similar to the following query:

MATCH (root:Root {key: 'abcde'})
MATCH p=(root)-[:r1|r2|r3|r4*..3]-(n:A|B|D|C {property: 'abc'})
UNWIND nodes(p) AS node
RETURN DISTINCT node.key AS name
ORDER BY name ASC

Additionally, how can I get all the distinct triples instead of the node names?

Thank you

Hi @low.dusk9102 ,

since D is connected to A this looks as an easy problem. You can do the following query that you have written yourself, by using the QPP: Concepts - Cypher Manual.

If you can write show how your graph looks and what you expect as a result, I could help you with a query that would solve the issues.

Try this:

MATCH (root:Root {key: 'abcde'})
MATCH p=(root)-[:r1|r2|r3|r4*..3]-(n:A|B|D|C)
WHERE not n:C or (n:C and n.property = 'abc')
UNWIND nodes(p) AS node
RETURN DISTINCT node.key AS name
ORDER BY name ASC

Note, you could also use a UNION query. One part queries for A, B, and D nodes. The other part for C nodes.