Complex relationship matching

Hello!

Thank you guys for being so helpful and responsive on these forums!

When running a query with edge matching on both sides I got different behavior.

Here is an example of the Match:

“MATCH (a:A {prop: “value1”})—>(b:B)-[*]->(c:C)<-[:E1*0..5]<—(d:C {prop: “value3”})
RETURN DISTINCT c.prop”

Vs two queries:

MATCH (a:A {prop: “value1”})—>(b:B)-[*]->(c:C) RETURN DISTINCT c.prop

Let props be the list returned from this query.

MATCH (d:C {prop:”value3”})-[:E1*0..5]->(c:C)
WHERE c.prop IN $props
RETURN DISTINCT c.prop

These give back different answers in my application although, to me, the queries seem to do the same thing. The second returns what I would expect but the first doesn’t seem to.

I can try and produce a minimum reproducible program for this but just wanted to see if anyone has input into why this might be occurring/If there’s an error in my thinking.

These queries are not the same, as the first one is more constraining, as it requires node c to be reachable from both a AND d while the other two queries only require only one connection, in the first phase you require c to be reachable from a and in the second phase you require c to be connected to d

Assume this is your graph:

CREATE (a:A {prop: “value1”})-[]->(b:B)-[]->(c:C {prop:1})
CREATE (d:C {prop:”value3”})-[:E1]->(c:C {prop:1})

The first query will come up short, while the second query will return both c nodes

  • i’ve removed the variable length path from simplification.

I’m sorry I’m not sure that I understand what you’re saying.

Is there a difference between being reachable vs being connected?

In my application, I’m comparing executing the first query (the double sided query) vs executing the two second queries in succession.

  • It seems to me that checking all the nodes that are reachable from both a and d should be the same as getting all the nodes that are reachable from a and then filtering out for only those that are reachable from d. Is there something wrong with my reasoning here?

Consider two ‘c’ nodes with the same prop value,
One reachable from the first pattern: (a:A {prop: “value1”})—>(b:B)-[*]->(c:C)
The other reachable from the second pattern: (d:C {prop:”value3”})-[:E1*0..5]->(c:C)

Your first query: “MATCH (a:A {prop: “value1”})—>(b:B)-[*]->(c:C)<-[:E1*0..5]<—(d:C {prop: “value3”}) RETURN DISTINCT c.prop”

Will not find any result, as there’s no c node which satisfy this entire pattern,
but the other 2 queries:

MATCH (a:A {prop: “value1”})—>(b:B)-[*]->(c:C) RETURN DISTINCT c.prop
MATCH (d:C {prop:”value3”})-[:E1*0..5]->(c:C)
WHERE c.prop IN $props
RETURN DISTINCT c.prop

will each find a different c node.