Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher query - query property array with regular expression

I have a Neo4j database (2.0.3). I am currently using Cypher to run some test queries on my dataset. I have a set of records that have a property containing an array of names. I want to be able to search this array of names, while also using a basic reg-ex to do so. Is this at all possible within cypher? If not how do you reccommend going about this?

Something that would combine both of these queries:

 MATCH (s:Record) WHERE "John" IN s.name RETURN s;
 MATCH (s:Record) WHERE s.name =~ '(?i).*john.*)' RETURN s;
like image 215
prees Avatar asked Jul 08 '14 18:07

prees


1 Answers

Consider the following example data:

CREATE (:Record {name: ['John', 'Bob']}),
       (:Record {name: ['Alice', 'Johnny']}),
       (:Record {name: ['the johnster', 'Charles']}),
       (:Record {name: ['Danny', 'Josh']})

If you want to find all Records where any of the elements in the property array name matches a regexp, use ANY. If you want to find all Records where all elements in the property array name match a regexp, use ALL. I believe you want the former:

MATCH (s:Record)
WHERE ANY(name IN s.name WHERE name =~ '(?i).*john.*')
RETURN s.name

This returns:

s.name
John, Bob
Alice, Johnny
the johnster, Charles
like image 69
Nicole White Avatar answered Sep 20 '22 15:09

Nicole White