简体   繁体   中英

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM