Hello,
I wrote a cypher query in Neo4J 4.4 which works as expected.
Its objective is:
- from a start node, identified by an id (p)
- returns all connected nodes (p2) of a type linked by relationship with specific conditions
- complete these result with relationships between p2 nodes with same conditions (equivalent to the UI "Connect result nodes" functionality but limited by the conditions)
Here is the query:
MATCH path=(p:Party {partyId: XXXX})-[]-(p2:Party)
WHERE <condition>
RETURN path
UNION
MATCH path2=(p:Party {partyId: XXXX})-[]-(p2:Party)
WHERE <condition>
WITH collect(p2) as n
UNWIND n as x
UNWIND n as y
MATCH path=(x)-[r]->(y)
WHERE <condition>
RETURN path
As you can see, the first sub-query is used twice and I would like to simplify/optimize it.
Is it possible ?
I tried this kind of query:
MATCH path=(p:Party {partyId:XXXX})-[]-(p2:Party)
WHERE <condition>
WITH path, collect(p2) as n
UNWIND n as x
UNWIND n as y
MATCH path2=(x)-[r]->(y)
WHERE <condition>
RETURN path,path2
but it returns no result. Even if I limit the last RETURN only to path or path2.
I don't really understand why.
Thank you in advance for all tips ...
Laurent.