I am querying a Neo4j database that stores building floorpans. An example of a query is:
MATCH (s:STOREYVERTEX) <-- (room0: LIVING)
MATCH (s) <-- (room1: DINING)
MATCH (s) <-- (room2: KITCHEN)
MATCH (room0) - [edge0: DOOR] -> (room2)
MATCH (room2) - [edge1: DOOR] -> (room1)
RETURN s
Now this returns all graphs that have a subgraph corresponding to the constraints. I would like to somehow limit the number of nodes that the result graph have, so for the given example I would like to get only the graphs that have exactly 3 nodes, all of type ROOM.
Is there a way to do this in Cypher?
Edit: Something like this does not work:
MATCH (s:STOREYVERTEX) <-- (rooms:ROOM)
WITH s, count(distinct(rooms)) as numberOfRooms
WHERE numberOfRooms = 3
MATCH (s) <-- (room1: DINING)
MATCH (s) <-- (room2: KITCHEN)
MATCH (room0) - [edge0: DOOR] -> (room2)
MATCH (room2) - [edge1: DOOR] -> (room1)
RETURN s
Yes, you can do that, like this:
MATCH (s:STOREYVERTEX) <-- (rooms:ROOM)
WITH s, count(distinct(rooms)) as numberOfRooms
WHERE numberOfRooms = 3
RETURN s;
This just checks how many distinct different rooms are connected to a STOREYVERTEX
, and only returns the s
values where that's 3.
You didn't specify what the DOOR
stuff was about in your query, but you should be able to modify this query from here to get where you want to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With