0
votes

I recently started using neo4j db and its spatial plugin.

My data model consist of 4 different nodes: Building, Floor, Room and Device. All of them related by the relationship "HAS".

The node added to the spatial layer is Building.

This is how it looks at this moment:

data model To get all the Buildings that intersect in a given polygon I'm using the next cypher

WITH 'POLYGON((-6.342523097991944 39.47879396554205, -6.339948177337647 
39.47879396554205, -6.339948177337647 39.478121118127696, 
-6.342523097991944 39.478121118127696, -6.342523097991944 
39.47879396554205))' as polygon
CALL spatial.intersects('geom',polygon) YIELD node
RETURN node

Particularity that returns one building:

enter image description here

Now, what I'm trying to do is to obtain the sub-graph holding from the intersected Building. Here is my try:

WITH 'POLYGON((-6.342523097991944 39.47879396554205, -6.339948177337647 
39.47879396554205, -6.339948177337647 39.478121118127696, -6.342523097991944 
39.478121118127696, -6.342523097991944 39.47879396554205))' as polygon
CALL spatial.intersects('geom',polygon) YIELD node
MATCH (node)-[:HAS]->(Floor)-[:HAS]->(Room)-[:HAS]->(Device)
RETURN *

That outputs the next graph:

enter image description here

That result is similar of what I want but not exactly. What I need is all the intersected building, all its floors, rooms and devices. This result is only giving my the floors that have rooms and the rooms that have devices. I don't know how to write the cypher to get what I need.

Apart from that, the previous cypher is taking around 7 seconds to output the results, is that time normal with the size of my actual graph? It seems a lot to me.

Thank you!

1
You didn't use labels in your query missing the leading colon : before the labels. aka (room:Room) - Michael Hunger
HAS is also a very generic relationship-type you might want to consider using something more expressive - Michael Hunger
Thank you @MichaelHunger, I followed your suggestions. - Nicolás González

1 Answers

0
votes

You can specify a variable length pattern:

...
CALL spatial.intersects('geom',polygon) YIELD node
MATCH (node)-[:HAS*0..3]->(Item)
RETURN Item