Ricardo
Ricardo

Reputation: 818

Cypher query to find a node based on a regexp on a property

I have a Neo4J database mapped with COMPANY as nodes and RELATED as edges. COMPANY has a CODE property. I want to query the database and get the first node that matches the regexp COMPANY.CODE =~ '12345678.*', i.e., a COMPANY whose first 8 letters of CODE is equal a given string literal.

After several attempts, the best I could come up with was the following query:

START p=node(*) where p.CODE =~ '12345678.*' RETURN p;

The result is the following exception:

org.neo4j.cypher.EntityNotFoundException: 
The property 'CODE' does not exist on Node[0]

It looks like Node[0] is a special kind of node in the database, that obviously doesn't have my CODE property. So, my query is failing because I'm not choosing the appropriate type of node to query upon. But I couldn't figure out how to specify the type of node to query on.

What's the query that returns what I want?

I think I need an index on CODE to run this query, but I'd like to know whether there's a query that can do the job without using such an index.

Note: I'm using Neo4J version 1.9.2. Should I upgrade to 2.0?

Upvotes: 2

Views: 643

Answers (1)

jjaderberg
jjaderberg

Reputation: 9952

You can avoid the exception by checking for the existence of the property,

START p=node(*)
WHERE HAS(p.CODE) AND p.CODE =~ '12345678.*'
RETURN p;

You don't need an index for the query to work, but an index may increase performance. If you don't want indices there are several other options. If you keep working with Neo4j 1.9.x you may group all nodes representing companies under one or more sorting nodes. When you query for company nodes you can then retrieve them from their sorting node and filter on their properties. You can partition your graph by grouping companies with a certain range of values for .code under one sorting node, and a different range under another; and you can extend this partitioning as needed if your graph grows.

If you upgrade to 2.0 (note: not released yet, so not suitable for production) you can benefit from labels. You can then assign a Company label to all those nodes and this label can maintain it's own index and uniqueness constraints.

The zero node, called 'reference node', will not remain in future versions of Neo4j.

Upvotes: 4

Related Questions