Reputation: 4173
What is the best way to find node A
if I know the nodes C
and B
in Cypher?
┌────HAS_ITEM───┐
│ ▼
│ .─.
│ ( C ) :ITEM
.─. `─'
( A )
`─'
│ .─.
└─HAS_ITEM───▶( B ) :ITEM
`─'
My current query uses a MATCH ... WITH ... MATCH
construction, but maybe there is way to express this in a better way?
Upvotes: 1
Views: 50
Reputation: 11216
You can just create a single expression that finds A
between both B
and C
CREATE INDEX ON :Item(name)
MATCH (b:Item {name: 'B'})<-[:HAS_ITEM]-(a)-[:HAS_ITEM]->(c:Item {name: 'C'})
RETURN a
If you had many items and you were looking for all of the items that shared that item you could do something like this.
// provide a list of values you want to match against
WITH ['B', 'C', 'D', 'E'] AS list
// find all of the matches
MATCH (a:Item)-[r:HAS_ITEM]->(b:Item)
WHERE b.name IN list
// collect the matched b nodes per a node
WITH list, a, collect(b) AS bs
// check to see if the matched number is the same as the input
WHERE size(bs) = size(list)
RETURN a
Upvotes: 3