如何在return语句

时间:2017-12-13 20:04:57

标签: neo4j cypher

以下是我想要的结果

  1. 返回图
  2. 中突出显示的结果
  3. 地方(作为所有者/室友)应为1& 2级连接(状态= 1)与MU,即使我不想包括MU位置(作为所有者/室友)
  4. 用户可以有多个地方
  5. 地方必须只有一个拥有者,可能有多个室友
  6. 结果应采用格式

    place, 
    [owner, is 1/2/3 degree connection],
    [housemates[housmate1(1/2/3 degree connection),housemate( is 1/2/3 degree connection)]])/
    

    这就是我现在所拥有的

    MATCH (n),
          (n)-[rels:`connected_to` {status: 1}]- (sp: `StayPal`),
          (sp)-[tenant0:owner_of|house_mate]-(place:`Place`),
          (place)-[tenant:owner_of|house_mate]-(c: `StayPal`) 
    WHERE (ID(n) = {ID_n}) AND NOT(n) - [:house_mate] - (place)
    WITH place,
       c,
       tenant, 
       CASE
       WHEN (n)-[:connected_to {status: 1}]-(c) THEN 's'
       WHEN (n)-[:connected_to*1..2 {status: 1}]-(c) THEN 'ss'
       ELSE 'sss'
       END AS connection 
    WITH 
       place,
       collect([tenant, c, connection]) AS tenants RETURN place,
               [tenant IN tenants WHERE type(tenant[0]) = 'owner_of'   | tenant][0] AS ownerArray,                               
               [tenant IN tenants WHERE type(tenant[0]) = 'house_mate' | tenant] AS houseMatesArray
    

    编辑2 样本数据集

    CREATE
      (mu:MU {name: 'MU1'}),
      (u1:User {number: 1}), (u2:User {number: 2}), (u3:User {number: 3}), (u4:User {number: 4}), (u5:User {number: 5}), (u6:User {number: 6}), (u7:User {number: 7}), (u8:User {number: 8}), (u9:User {number: 9}),
      (mu)-[:connected_to {status: 1}]->(u1),
      (mu)-[:connected_to {status: 1}]->(u2)-[:connected_to {status: 1}]->(u3),
      (mu)-[:connected_to {status: 1}]->(u8),
      (mu)-[:connected_to]->(u9),
      (u4)-[:connected_to {status: 1}]->(u3),
      (u8)-[:connected_to {status: 1}]->(u5),
      (pA:Place {name: 'A'}), (pB:Place {name: 'B'}), (pC:Place {name: 'C'}), (pD:Place {name: 'D'}), (pE:Place {name: 'E'}),
      (mu)<-[:house_mate]-(pA)-[:owner_of]->(u1),
      (u4)<-[:house_mate]-(pB)-[:owner_of]->(u3),
      (u8)<-[:house_mate]-(pC)-[:owner_of]->(u5),
      (u9)<-[:house_mate]-(pD)
    

1 个答案:

答案 0 :(得分:1)

试试这个:

#fragment

此查询执行其他 MATCH (n {name: 'MU1'})-[:connected_to*1..2 {status: 1}]->(sp:User), (sp)<-[:owner_of|house_mate]-(place:Place)-[:owner_of|house_mate]->(c:User) WHERE NOT (n)-[:house_mate]-(place) MATCH (place)-[tenant:owner_of|house_mate]->(u:User), (u)-[rels:connected_to*1..3 {status: 1}]-(n) WITH DISTINCT place, type(tenant) AS type, u, size(rels) AS connection WITH place, collect({type: type, u: u, connection: connection}) AS tenants RETURN place, [tenant IN tenants WHERE tenant.type = 'owner_of' | [tenant.u, tenant.connection]][0] AS owner, [tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u, tenant.connection]] AS houseMatesArray ,以便它可以将MATCH - &gt; placesp中的用户收集到一个地点(在变量place->c下)。

它返回:

u