1

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?

Mik378
  • 21,881
  • 15
  • 82
  • 180

1 Answers1

3

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.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • Nice! :) So is it more efficient than the second solution I wrote in my "update" section? – Mik378 Aug 22 '14 at 10:34
  • 1
    It depends on the number of nodes found by the spatial query versus the number of nodes having the Race label. The query I've sketched does a index query and looks up all the nodes referenced by the result of the index query. So I consider it being more efficient in most cases. – Stefan Armbruster Aug 22 '14 at 10:35
  • If you do `START c=node:carslocation .... MATCH (cr:Race) WHERE ....` you're doing the index query, load all nodes for its result *AND* look up all nodes carrying the `Race` label globally. – Stefan Armbruster Aug 22 '14 at 10:37