How can I make a string contain filter on Neo4j Cypher
Asked Answered
N

2

24

I need to make a string contain filter in Neo4J. The idea is simple.

A good example is that I need to retrieve from a database of persons all the people that contain in his name the car substring.

How can I do this?

Newbill answered 7/6, 2014 at 7:26 Comment(0)
C
22

You can use regular expressions to match a part of a name, for example:

MATCH (n)
WHERE n.name =~ '.*car.*'
RETURN n

If you have the label 'Person' assigned to all people in your database, the query would be:

MATCH (n:Person)
WHERE n.name =~ '.*car.*'
RETURN n

For further information, see http://docs.neo4j.org/chunked/stable/query-where.html#_regular_expressions

Carleencarlen answered 7/6, 2014 at 11:39 Comment(0)
L
36

As an additional update, from neo4j 3.0 it may be more readable to use:

MATCH(n)
WHERE n.name CONTAINS 'car'
RETURN n

(Edited to include Maciej fix to my response, thank you!)

Lefkowitz answered 19/8, 2016 at 11:20 Comment(2)
This approach works but you must surround car in quotes like so: MATCH(n) WHERE n.name CONTAINS 'car' RETURN nCharitycharivari
Can you use contains with a list of words?Anthemion
C
22

You can use regular expressions to match a part of a name, for example:

MATCH (n)
WHERE n.name =~ '.*car.*'
RETURN n

If you have the label 'Person' assigned to all people in your database, the query would be:

MATCH (n:Person)
WHERE n.name =~ '.*car.*'
RETURN n

For further information, see http://docs.neo4j.org/chunked/stable/query-where.html#_regular_expressions

Carleencarlen answered 7/6, 2014 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.