0
votes

I have a Neo4j node called entity which can be connected to other entities. I want to write a cypher query where I specify a list of entity ids and it brings me back those entities and how they are connected.

For example:

enter image description here

So if I specify Entity A and Entity B ids, it should bring me all other entities including mutual relationships (c-d) and interconnecting entity E.

Could someone help me with the cypher query?

1

1 Answers

1
votes

Assuming that idList is an input parameter that defines a list of neo4j ids for entities you want to see in the result, this should return what you need:

MATCH (a:Entity)-[r:RELATED_TO]->(b:Entity)
WHERE id(a) IN $idList AND id(b) IN $idList
RETURN a, r, b

Note that isolated nodes will not be shown in the result. If you want to inlculde those too, you can do this instead:

MATCH (a:Entity),(b:Entity)
WHERE id(a) IN $idList AND id(b) IN $idList
OPTIONAL MATCH (a)-[r:RELATED_TO]->(b)
RETURN a, r, b