Hello, I have the following structure:
(building)-[:HAS]->(:Floor)-[:HAS]-(:Room)-[:HAS_OBJECT]->(:Object),
(:Room)-[:HAS_REQUIREMENT]-(:Requirement)
I've attached an image of the dataset, and a create script at the bottom
I would like to output this data as a json object, but with a few constraints
{
"Name": "Building one",
"floors": [
{
"rooms": [
{
"requirements": [
{
"Name": "Three requirement 2"
},
{
"Name": "Three requirement"
}
],
"Name": "Three",
"objects": [
{
"Name": "Three object 2"
},
{
... etc
So the path to match is:
Give me all buildings, with their floors, with their rooms, with their objects and requirements.
And now i want to be able to constraint my data, e.g.: only give me two rooms per floor, ordered by room.Name.
Another case would be be: give me the two biggest rooms on the floor.
I've tried a lot of things, but it just seems impossible in Neo4J.
I tried:
- Map projections (Maps - Cypher Manual) . Using map projections doesn't work because aggregating functions don't work in the constraint, also, limiting the resultset cannot be done
- apoc.convert.toTree. I can't get apoc.convert.toTree to work at the query part where the query splits (room)->(object), (room)->(requirement). I've tried chaining the different paths together, but I can't get that to work at the point were the query splits (at the room node)
- subqueries using CALL {}. This doesnt work because I can't pass variables from the outer scope to the subquery.
- A combination of unwind / collect becomes doesn't work in combination where some rooms don't have requirements, but do have objects.
It seems like a very trivial task, but I can't get it to work. So, any help with this would be greatly appreciated.
UNWIND [{_id:1, properties:{NumberOfRooms:5, Name:"First floor"}}, {_id:8, properties:{NumberOfRooms:0, Name:"Floor X"}}, {_id:9, properties:{NumberOfRooms:1, Name:"Floor Y"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Floor;
UNWIND [{_id:12, properties:{Name:"Zero requirement"}}, {_id:13, properties:{Name:"One requirement"}}, {_id:14, properties:{Name:"Two requirement"}}, {_id:15, properties:{Name:"Three requirement"}}, {_id:18, properties:{Name:"Zero requirement 2"}}, {_id:19, properties:{Name:"One requirement 2"}}, {_id:20, properties:{Name:"Two requirement 2"}}, {_id:21, properties:{Name:"Three requirement 2"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Requirement;
UNWIND [{_id:0, properties:{Name:"Building one"}}, {_id:7, properties:{Name:"Building two"}}, {_id:11, properties:{Name:"Empty building"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Building;
UNWIND [{_id:22, properties:{Name:"Zero object"}}, {_id:23, properties:{Name:"One object"}}, {_id:24, properties:{Name:"Two object"}}, {_id:25, properties:{Name:"Three object"}}, {_id:26, properties:{Name:"Four object"}}, {_id:27, properties:{Name:"Room second building floor Y object"}}, {_id:28, properties:{Name:"Zero object 2"}}, {_id:29, properties:{Name:"One object 2"}}, {_id:30, properties:{Name:"Two object 2"}}, {_id:31, properties:{Name:"Three object 2"}}, {_id:32, properties:{Name:"Four object 2"}}, {_id:33, properties:{Name:"Room second building floor Y object 2"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Object;
UNWIND [{_id:2, properties:{Depth:4.8, Name:"Zero"}}, {_id:3, properties:{Area:10.8, Width:3, Depth:3.5999999999999996, Name:"One"}}, {_id:4, properties:{Area:10.8, Width:3, Depth:3.5999999999999996, Name:"Two"}}, {_id:5, properties:{Area:30, Width:5, Depth:6, Name:"Three"}}, {_id:6, properties:{Area:19.2, Width:4, Depth:4.8, Name:"Four"}}, {_id:10, properties:{Width:4.4, Depth:2.2, Name:"Room second building floor Y"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Room;
UNWIND [{start: {_id:0}, end: {_id:1}, properties:{}}, {start: {_id:7}, end: {_id:8}, properties:{}}, {start: {_id:7}, end: {_id:9}, properties:{}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
CREATE (start)-[r:HAS]->(end) SET r += row.properties;
UNWIND [{start: {_id:2}, end: {_id:22}, properties:{}}, {start: {_id:3}, end: {_id:23}, properties:{}}, {start: {_id:4}, end: {_id:24}, properties:{}}, {start: {_id:5}, end: {_id:25}, properties:{}}, {start: {_id:6}, end: {_id:26}, properties:{}}, {start: {_id:10}, end: {_id:27}, properties:{}}, {start: {_id:2}, end: {_id:28}, properties:{}}, {start: {_id:3}, end: {_id:29}, properties:{}}, {start: {_id:4}, end: {_id:30}, properties:{}}, {start: {_id:5}, end: {_id:31}, properties:{}}, {start: {_id:6}, end: {_id:32}, properties:{}}, {start: {_id:10}, end: {_id:33}, properties:{}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
CREATE (start)-[r:HAS_OBJECT]->(end) SET r += row.properties;
UNWIND [{start: {_id:2}, end: {_id:12}, properties:{}}, {start: {_id:3}, end: {_id:13}, properties:{}}, {start: {_id:4}, end: {_id:14}, properties:{}}, {start: {_id:5}, end: {_id:15}, properties:{}}, {start: {_id:2}, end: {_id:18}, properties:{}}, {start: {_id:3}, end: {_id:19}, properties:{}}, {start: {_id:4}, end: {_id:20}, properties:{}}, {start: {_id:5}, end: {_id:21}, properties:{}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
CREATE (start)-[r:HAS_REQUIREMENT]->(end) SET r += row.properties;
UNWIND [{start: {_id:1}, end: {_id:2}, properties:{}}, {start: {_id:1}, end: {_id:3}, properties:{}}, {start: {_id:1}, end: {_id:4}, properties:{}}, {start: {_id:1}, end: {_id:5}, properties:{}}, {start: {_id:1}, end: {_id:6}, properties:{}}, {start: {_id:9}, end: {_id:10}, properties:{}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
CREATE (start)-[r:HAS]->(end) SET r += row.properties;
MATCH (n:`UNIQUE IMPORT LABEL`) WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;