Get Search nodes and their relationship with those nodes as well

I have a search query for a property which can be achieved by the following cypher query

MATCH (n: USER) WHERE n.fullname CONTAINS 'q' RETURN n

Say it matches 3 USER nodes and returns those 3

Then I want to see if there is a relationship with those nodes which is achieved by the following query which is handled by a for loop in python

MATCH (n: USER{userid: 'U1'})-[r:FOLLOWING]->(m:USER{userid: 'RESULT_USERID'}) RETURN m
(Executed 3 times for 3 results)

I want to do both of this in single query. How can it be achieved?

Thanks,
Skanda

Hi Skanda,

You can easily achieve this using 'WITH' to carry forward the result from the first query and use it into the second query.

Something like this-
MATCH (n: USER) WHERE n.fullname CONTAINS 'q'
WITH n
MATCH (n)-[r:FOLLOWING]->(m:USER{userid: 'RESULT_USERID'}) RETURN m;

This would show the same result as you expect under this one singe query.

1 Like

Perfect! Thanks for your quick inputs Dhurv.