I have a graph with lots of distinct labels. I now want to select all nodes of a given subset of labels and all relationships between them.
I was able to get the expected result with the solution from How to get all relationships between a set of nodes?:
MATCH (m:A|B|C)-[r]->(n:A|B|C)
RETURN m, r, n
but this feels too repetitive. How can I avoid specifying the set of labels, i.e. A|B|C, twice?
I tried the following, but it doesn't return all relationships, but only those between a node and itself:
MATCH (n:A|B|C)-[r]->(n)
RETURN r, n
I'm looking for something like $labels = :A|B|C MATCH (m:$labels)-[r]->(n:$labels)
Sample data:
CREATE (a:A)-[:R]->(b:B)<-[:R]-(c:C)<-[:R]-(c)<-[:R]-(a)-[:R]->(d:D)
I want the query to return:
- (a)->(b)
- (c)->(b)
- (c)->(c)
- (a)->(c)
Unfortunatley, dynamic labels are not a thing in Cypher, yet.
The following query is a lot more verbose but it does not have the repetition. So, depending on your label expression, that may pay off: