Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regular expression in neo4j

I'm trying to use regular expressions in a cypher WHERE clause. I would like to match nodes in which a node's property Text contains a specific word, as a word and not part of it.

MATCH (n:) WHERE n.Text =~ '\bword\b' return n;

This query doesn't return anything although nodes containing the word "word" exist in my graph. Does cypher allow the use of standard regular expressions? Are there limitations in its regular expression implementation?

like image 821
Pierre Avatar asked May 23 '14 16:05

Pierre


People also ask

What is a clause in Neo4j?

Administration clauses These comprise clauses used to manage databases, schema and security; further details can found in Database management and Access control. Clause. Description. CREATE | DROP | START | STOP DATABASE. Create, drop, start or stop a database.

How do you create a relationship between two nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

Is null in Neo4j?

In Neo4j, since there is no table schema or equivalent to restrict possible properties, non-existence and null are equivalent for node and relationship properties. That is, there really is no such thing as a property with a null value; null indicates that the property doesn't exist at all.

What is optional match in Neo4j?

An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.


1 Answers

There were 3 problems in your query:

  1. (n:) should be (n), since you are not specifying a label.
  2. Back slashes must be escaped by a preceding back slash.
  3. The regex needs to match the entire property value (I assume that your test value was fairly long).

This query should work:

MATCH (n)
WHERE n.Text =~ '.*\\bword\\b.*'
RETURN n;

See here for documentation related to Regular Expressions in Neo4j

like image 132
cybersam Avatar answered Oct 23 '22 03:10

cybersam