数据集
CREATE
(u1:User {number: 1}), (u2:User {number: 2}),
(r1:Room {name: 'r1'}), (r2:Room {name: 'r2'}),
(d1:UnavailableDate {date: '1/1/2016'}), (d2:UnavailableDate {date: '1/2/2016'}),
(i1:Image {url: 'http://..'}), (i2:Image {url: 'http://..'}),(i3:Image {url: 'http://..'}),
(pA:Place {name: 'P'}),
(u1)<-[:house_mate]-(pA)-[:owner_of]->(u2),
(pA)<-[:place]-(r1),
(pA)<-[:place]-(r2),
(r1)<-[:room]-(d1),
(r1)<-[:room]-(d2),
(r2)<-[:room]-(i1),
(r2)<-[:room]-(i2),
(r1)<-[:room]-(i3)
以下是我的查询
MATCH (place:`Place` {name: 'P'}),
(place)-[:place]-(room:Room)
OPTIONAL MATCH (place)-[tenant:owner_of|house_mate]-(u:User)
OPTIONAL MATCH (room)-[:room]-(date:UnavailableDate)
OPTIONAL MATCH (room)-[:room]-(image:Image)
WITH DISTINCT place,
collect(room) AS r,
collect(image) AS images,
collect(date) AS dates,
type(tenant) AS type,
u
WITH place,
collect({type: type, u: u}) AS tenants,
collect({rooms: r, images: images, dates: dates}) AS rooms
RETURN DISTINCT place,
rooms,
[tenant IN tenants WHERE tenant.type = 'owner_of' | [tenant.u]][0] AS owner_array,
[tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]] AS house_mates_array
问题是收集图像&amp;日期应该在房间不在场
任何帮助?
答案 0 :(得分:3)
重复项来自背对背的可选竞赛。您正在为u
,date
和image
的所有组合获取跨产品。为避免这种情况,您需要在每个之后立即收集。
或者,更好的是,使用pattern comprehension立即将结果收集到一个集合中,并在最终集合中使用map projection将日期和图像添加到每个关联的房间中:
MATCH (place:`Place` {name: 'P'})
WITH place, [(place)-[:owner_of]-(u:User) | u] as owner_array, [(place)-[:house_mate]-(u:User) | u] as house_mates_array
MATCH (p)-[:place]-(room:Room)
WITH place, owner_array, house_mates_array, room, [(room)-[:room]-(date:UnavailableDate) | date] as dates, [(room)-[:room]-(image:Image) | image] as images
RETURN place, collect(room {.*, dates, images}) as rooms, owner_array, house_mates_array
答案 1 :(得分:1)
这可能接近你想要的:
MATCH (place:`Place` {name: 'P'})-[:place]-(room:Room)
OPTIONAL MATCH (place)-[tenant:owner_of|house_mate]-(u:User)
WITH place, room, TYPE(tenant) AS type, u
OPTIONAL MATCH (room)-[:room]-(date:UnavailableDate)
OPTIONAL MATCH (room)-[:room]-(image:Image)
WITH place, room,
collect(image) AS images,
collect(date) AS dates,
collect(DISTINCT {type: type, u: u}) AS tenants
RETURN place,
collect({room: room, images: images, dates: dates}) AS room_data,
[tenant IN tenants WHERE tenant.type = 'owner_of' | [tenant.u]][0] AS owner_array,
[tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]] AS house_mates_array;
结果如下:
╒════════════╤══════════════════════════════════════════════════════════════════════╤══════════════╤═══════════════════╕
│"place" │"room_data" │"owner_array" │"house_mates_array"│
╞════════════╪══════════════════════════════════════════════════════════════════════╪══════════════╪═══════════════════╡
│{"name":"P"}│[{"room":{"name":"r2"},"images":[{"url":"http://.."},{"url":"http://..│[{"number":2}]│[[{"number":1}]] │
│ │"},{"url":"http://.."},{"url":"http://.."}],"dates":[]},{"room":{"name│ │ │
│ │":"r1"},"images":[{"url":"http://.."},{"url":"http://.."},{"url":"http│ │ │
│ │://.."},{"url":"http://.."}],"dates":[{"date":"1/1/2016"},{"date":"1/2│ │ │
│ │/2016"},{"date":"1/1/2016"},{"date":"1/2/2016"}]}] │ │ │
└────────────┴──────────────────────────────────────────────────────────────────────┴──────────────┴───────────────────┘