cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Match a node only if ALL specific connected nodes have a specific label

admin3
Node Clone

I think this is a newbie question but cannot sort it out.

Me unfinished query looks like this:

MATCH (manufacturer:Manufacturer)-[:HAS_BRAND]->(brand:Brand)
// Here I need something to filter manufacturers where ALL Brand nodes have the label Ready.
// If at least one 'Brand' node is not 'Ready' then don't return those manufacturers.
RETURN DISTINCT manufacturer

Thanks

1 ACCEPTED SOLUTION

We can do this with pattern comprehensions (which let us get the results of a MATCH in a list) and the all() list predicate:

MATCH (manufacturer:Manufacturer)
WHERE all(brand in [(manufacturer)-[:HAS_BRAND]->(brand) | brand] WHERE brand:Ready)
RETURN manufacturer

View solution in original post

3 REPLIES 3

We can do this with pattern comprehensions (which let us get the results of a MATCH in a list) and the all() list predicate:

MATCH (manufacturer:Manufacturer)
WHERE all(brand in [(manufacturer)-[:HAS_BRAND]->(brand) | brand] WHERE brand:Ready)
RETURN manufacturer

Hi,

I would like to extend my question.

I want to check 2 fields together simultaneously such as

MATCH (manufacturer:Manufacturer)
WHERE all(brand in [(manufacturer)-[:HAS_BRAND]->(brand)<-[:HAS_PART]-(part2), (brand)<-[:HAS_PART]-(part1) | [part1, part2]] WHERE part1:Ready AND part2:Ready)
RETURN manufacturer

The above doesn't really work as I am getting this error

Invalid input '|': expected whitespace, comment, a relationship pattern, '.', node labels, '[', '^', '*', '/', '%', '+', '-', "=~", IN, STARTS, ENDS, CONTAINS, IS, '=', '~', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, ',' or ']' 

Basically the issue is contructing this array when it is required to do more than 1 match

[(manufacturer)-[:HAS_BRAND]->(brand)<-[:HAS_PART]-(part2), (brand)<-[:HAS_PART]-(part1) | [part1, part2]]

Thanks!

I found a long workaround

MATCH (manufacturer:Manufacturer)

WITH manufacturer,
[[(manufacturer)-[:HAS_BRAND]->(brand)<-[:HAS_PART]-(part2) | part2],[(manufacturer)-[:HAS_BRAND]->(brand)<-[:HAS_PART]-(part1) | part1]] as transposedList

UNWIND range(0,size(transposedList[0])-1) as idx
WITH [transposedList[0][idx],transposedList[1][idx]] as adjustedSeries , manufacturer

WITH collect(adjustedSeries) as adjustedList, manufacturer
....