Return nodes with dynamic set of labels passed from previous match

Hi
i have this code
match ( n{ name:"Occ82"})
with labels(n) as lbl
return lbl

it returns
["Occurrence", "OccurenceSH", "Sport"]

i want to get all of the nodes that have any of above 3 labels

something like below but it is returning no node

match ( n{ name:"Occ82"})
with labels(n) as lbl
unwind lbl as l
match ( p:l) return p

That query is actually looking for nodes with label 'l', as labels can not be variables.

Try this:

match (n{name:"Occ82"})
match (p)
where any(i in labels(p) where i in labels(n))
return p

Hello @shsamardar :slight_smile:

This one should be faster:

MATCH (n {name: "Occ82"})
CALL apoc.cypher.run("MATCH (m) WHERE "+apoc.text.join([label IN labels(n) | "m:"+label], " OR ")+" RETURN m", {})
YIELD value
RETURN value

You can add a LIMIT after the RETURN m if needed.

Best regards,
Cobra

Thanks Cobra and Gary

it is working but :

for this code, what is the actual expanded string that will be executed .
is it like this

assuming i have [lbl1,lbl2, lbl3 ]

match m where m.lbl1 OR m.lbl2 OR m.lbl3 return m

this is not working .
what is the string that apoc.text.join is generating ?

Sorry I did not understand your question.

my question is

for the dynamic query that is been generated by apoc.text.join
what is that actual query ?

is it "match m where m.lbl1 OR m.lbl2 OR m.lbl3 return m"
to find labels that are either lbl1,lbl2,lbl3 a example ?

The final query will be:

MATCH (n {name: "Occ82"})
CALL apoc.cypher.run("MATCH (m) WHERE m.label1 OR m.label2 OR m.label3 RETURN m", {})
YIELD value
RETURN value

Best regards,
Cobra

Thanks Cobra

i want to do a match on a dynamic relationship type

match ( u:RevisionRule)
with u.name as revrule

revrule return "Latest Working" . it has space.

whei i execute below

match ( u:RevisionRule)
with u.name as revrule
call apoc.cypher.run ( "match ( o:Occurrence ) - [r:"+revrule+"] ->(p:ProductRevision) return o ,p", {} ) yield value
with value , revrule
with revrule, value.o as o , apoc.text.split( value.o.occurrenceRefs, " ") as childOccurrence , value.p as p
unwind childOccurrence as child
return child, p , o

i get this error

Failed to invoke procedure apoc.cypher.run: Caused by: org.neo4j.exceptions.SyntaxException: Invalid input 'Working': expected
"*"
"WHERE"
"]"
"{"
a parameter (line 1, column 36 (offset: 35))
"match ( o:Occurrence ) - [r:Latest Working] ->(p:ProductRevision) return o ,p"

the pointer is in Latest Working .i am guessing it is because of space in relationship type.

what do you suggest?

Hello @shsamardar :slight_smile:

If you have a space in a relationship type or a node label then you must use a single quote:

MATCH (u:RevisionRule) 
WITH u.name AS revrule 
CALL apoc.cypher.run("MATCH (o:Occurrence)-[r:`"+revrule+"`]->(p:ProductRevision) RETURN o, p", {}) 
YIELD value 
WITH value, revrule 
WITH revrule, value.o AS o, apoc.text.split(value.o.occurrenceRefs, " ") AS childOccurrence, value.p AS p 
UNWIND childOccurrence AS child 
RETURN child, p, o

You can find more details here.

Best regards,
Cobra