Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j 2.0 wildcard search on label names

When dealing with properties in Cypher, you can use regular expressions to match property values like so:

Match (n)-[:IS_A]-() where (n:Course_Driving_001) and (n.name =~ '(?i).*criteria.*' or n.description =~ '(?i).*criteria.*')   return distinct n limit 20;

I'd like to do the same thing with a label name. I'd like to get all unique labels that contain a certain string. Something like:

 Match (n)-[:IS_A]-() where (n:Course_*_001) return distinct n;

Can this be done is Cypher? or the RestAPI? Regular expressions?

I'm using Neo4j 2.0 Release.

like image 789
Lisa Kester Avatar asked Dec 17 '13 23:12

Lisa Kester


1 Answers

You cannot directly use regex on labels. However using the labels function this is possible:

MATCH (n)-[:IS_A]->() 
WHERE any(l IN labels(n) WHERE l=~'Course_*_001')
RETURN distinct n;

Please be aware that this query is potentially expensive as it touches all nodes in your db. You might want to refactor your data model to use multiple labels, e.g. Course and Course_xyz_001. In this case you can use MATCH (n:Course) .... which reduces the number of nodes to visit in first place.

like image 130
Stefan Armbruster Avatar answered Oct 24 '22 06:10

Stefan Armbruster