45

Is it possible to run a case-insensitive cypher query on neo4j?

Try that: http://console.neo4j.org/

When I type into this:

start n=node(*) 
match n-[]->m 
where (m.name="Neo") 
return m

it returns one row. But when I type into this:

start n=node(*) 
match n-[]->m 
where (m.name="neo") 
return m

it does not return anything; because the name is saved as "Neo". Is there a simple way to run case-insensitive queries?

gzg
  • 1,469
  • 6
  • 23
  • 39

4 Answers4

60

Yes, by using case insensitive regular expressions:

WHERE m.name =~ '(?i)neo'

https://neo4j.com/docs/cypher-manual/current/clauses/where/#case-insensitive-regular-expressions

vaer-k
  • 10,923
  • 11
  • 42
  • 59
Volker Pacher
  • 1,827
  • 18
  • 8
10

Another way would be:

WHERE LOWER(m.Name) = LOWER("Neo")

And if you are using the Neo4j Client (.NET):

Client.Cypher.Match("(m:Entity)")
    .Where("LOWER(m.Name) = LOWER({name})")
    .WithParam("name", inputName)
    .Return(m => m.As<Entity>())
    .Results
    .FirstOrDefault();
rotgers
  • 1,992
  • 1
  • 15
  • 25
2

If anybody is looking on how to do this with a parameter, I managed to do it like this.

query = "{}{}{}".format('Match (n) WHERE n.pageName =~ "'"(?i)", name, '" RETURN n')

and "name" is the variable or your parameter

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Can you explain how this works, or even how to read it? – mcv Jul 30 '20 at 22:04
  • `"{}{}{}"` looks like placeholders and `.format` is going to take in arguments in order or replacing placeholders. Readability is definitely one issue here but the larger issue seems to be this is just string concatenation not parametrisation. So in I could just do this `$"Match (n) WHERE n.pageName =~ {(?i)} name RETURN n` (C# string interpolation) – Piotr Kula Oct 09 '20 at 08:40
0

You can pass a parameter to the case insensitive regular expression like:

WHERE m.name =~'(?i)({param})