Search code examples
neo4jcypherneo4j-spatial

Neo4j / Cypher / Traversing nodes having a specific label


I'm using Neo4j Spatial plugin.

Let's suppose this basic query:

START c = node:carslocation('withinDistance:[2.3725963,48.892067, 34.0]') 
RETURN c

It returns all the cars within 34 km.

What if I want to only retrieve race cars (using Labels).

Without Spatial I would do:

MATCH (c:Race)
RETURN c 

Now if I want all race cars within 34km, I would have:

START c = node:carslocation('withinDistance:[2.3725963,48.892067, 34.0]') 
MATCH (c:Race)
RETURN c

=> Cannot add labels or properties on a node which is already bound.
Indeed, c was already bound at the first line.

I don't want to have to do this:

START c = node:carslocation('withinDistance:[2.3725963,48.892067, 34.0]') 
MATCH (cr:Race)
WHERE cr.id = c.id
RETURN cr

It would force me to open all nodes, to check for the equality... => bad performance especially when the query gets more complex ( even if id is indexed).

What could be an efficient solution?

UPDATE -------------------

Perhaps comparing nodes only is more efficient:

START c = node:carslocation('withinDistance:[2.3725963,48.892067, 34.0]') 
MATCH (cr:Race)
WHERE cr = c
RETURN cr

Is it?


Solution

  • How about:

    START c = node:carslocation('withinDistance:[2.3725963,48.892067, 34.0]') 
    WHERE 'Race' in labels(c)
    RETURN c
    

    You basically lookup all nodes within the given region and then filter them if their labels contain Race.