Neo4j将列表[L1 {e1,e2,e3 ...,eN}]映射到->列表的集合[E1 [e1.1,e1.2,e1.n],E2 [e2.1,e2 .2,e2.n],E3 [e3.1,e3.2,e3.n] ... EN [en.1,en.2,en.n]]

时间:2018-07-21 20:56:36

标签: sorting filter neo4j cypher nested-lists

在此用例中,请获得您的帮助。非常感谢。

这是如何测试用例

CREATE (r1:R{r:'1'})
CREATE (c1:C{cName:'1.1'})
CREATE (s1:S{sName:'1.1.1'})
CREATE (s2:S{sName:'1.1.4'})
CREATE (s3:S{sName:'1.1.3'})
CREATE (s1)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s1)-[:IS_A_S_BELONGING_TO_THAT_C]->(c1)
CREATE (s2)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s2)-[:IS_A_S_BELONGING_TO_THAT_C]->(c1)
CREATE (s3)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s3)-[:IS_A_S_BELONGING_TO_THAT_C]->(c1)
CREATE (c2:C{cName:'1.5'})
CREATE (s4:S{sName:'1.5.7'})
CREATE (s5:S{sName:'1.5.3'})
CREATE (s6:S{sName:'1.5.6'})
CREATE (s4)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s4)-[:IS_A_S_BELONGING_TO_THAT_C]->(c2)
CREATE (s5)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s5)-[:IS_A_S_BELONGING_TO_THAT_C]->(c2)
CREATE (s6)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s6)-[:IS_A_S_BELONGING_TO_THAT_C]->(c2)
CREATE (c3:C{cName:'1.2'})
CREATE (c1)-[:IS_A_C_BELONGING_TO]->(r1)
CREATE (c2)-[:IS_A_C_BELONGING_TO]->(r1)
CREATE (c3)-[:IS_A_C_BELONGING_TO]->(r1)
CREATE (s7:S{sName:'1.2.7'})
CREATE (s8:S{sName:'1.2.4'})
CREATE (s9:S{sName:'1.2.8'})
CREATE (s7)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s7)-[:IS_A_S_BELONGING_TO_THAT_C]->(c3)
CREATE (s8)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s8)-[:IS_A_S_BELONGING_TO_THAT_C]->(c3)
CREATE (s9)-[:IS_A_S_BELONGING_TO_THAT_R]->(r1)
CREATE (s9)-[:IS_A_S_BELONGING_TO_THAT_C]->(c3)
CREATE (r3:R{r:'7'})
CREATE (c4:C{cName:'7.1'})
CREATE (c5:C{cName:'7.4'})
CREATE (c6:C{cName:'7.3'})
CREATE (c4)-[:IS_A_C_BELONGING_TO]->(r3)
CREATE (c5)-[:IS_A_C_BELONGING_TO]->(r3)
CREATE (c6)-[:IS_A_C_BELONGING_TO]->(r3)
CREATE (r2:R{r:'9'})
CREATE (c7:C{cName:'9.7'})
CREATE (c8:C{cName:'9.2'})
CREATE (c9:C{cName:'9.4'})
CREATE (c7)-[:IS_A_C_BELONGING_TO]->(r2)
CREATE (c8)-[:IS_A_C_BELONGING_TO]->(r2)
CREATE (c9)-[:IS_A_C_BELONGING_TO]->(r2)

这将创建3套

R={"1","9","7"}

C={
"1.1","1.5","1.2"--------BELONGS TO "1" 
"7.1","7.4","7.3"--------BELONGS TO "7" 
"9.7","9.2","9.4"--------BELONGS TO "9"
}

S={
"1.1.1","1.1.4","1.1.3"  --------BELONGS TO "1.1" 
"1.5.7","1.5.3","1.5.6"  --------BELONGS TO "1.5" 
"1.2.7","1.2.4","1.2.8"  --------BELONGS TO "1.2"
.................................etc 
}

以下查询它不会导致我想要的结果:

MATCH (c:C)
MATCH (r:R)
WHERE 
(c)-[:IS_A_C_BELONGING_TO*]->(r)
WITH r
MATCH (c:C)-[:IS_A_C_BELONGING_TO*]->(r)<-[:IS_A_S_BELONGING_TO_THAT_R*]-(s:S)-[:IS_A_S_BELONGING_TO_THAT_C*]->(c)
WITH r,collect(DISTINCT [c]) as cList, collect(DISTINCT [s]) as sList
with r.r as rName, 
extract(nclist IN cList | [c in nclist | c.cName]) as cNameList,
extract(nlist IN sList | [s in nlist | s.sName]) as sNameList 
ORDER BY rName
UNWIND cNameList as x
WITH   sNameList,rName,x ORDER BY x 
UNWIND sNameList as y
WITH   x,rName,y ORDER BY y
return rName as rNames,collect(DISTINCT x) as cNames,collect(DISTINCT y) as sNames ORDER BY rName

结果

"1"---------------- {["1.1"],["1.2"],["1.5"]}--------------{[["1.1.1"],["1.1.3"],["1.1.4"], ["1.2.4"],["1.2.7"],["1.2.8"],["1.5.3"],["1.5.6"],["1.5.7"]]}

What I want is a List of sorted List with sorted elements  
=========================================================

    "1"---------------- {["1.1"],["1.2"],["1.5"]}--------------{[["1.1.1","1.1.3","1.1.4"], ["1.2.4","1.2.7","1.2.8"],["1.5.3","1.5.6","1.5.7"]]}


means ["1.1.1","1.1.3","1.1.4"] belongs to ["1.1"]  
      ["1.2.4","1.2.7","1.2.8"] belongs to ["1.2"]
      ["1.5.3","1.5.6","1.5.7"] belongs to ["1.5"]

可能有帮助的查询

要查找所有R = X示例为'1'的相关节点

MATCH (r:R)<-[:IS_A_C_BELONGING_TO*]-(c:C)<-[:IS_A_S_BELONGING_TO_THAT_C*]
                     -(s:S)-[:IS_A_S_BELONGING_TO_THAT_R*]->(r)
where r.r='1' 
return r,c,s

删除我的用例创建的所有节点

  MATCH (r:R)<-[:IS_A_C_BELONGING_TO*]-(c:C)<-[:IS_A_S_BELONGING_TO_THAT_C*]
                         -(s:S)-[:IS_A_S_BELONGING_TO_THAT_R*]->(r)
    where r.r='1' 
    DETACH DELETE r,c,s

    MATCH (r:R)<-[:IS_A_C_BELONGING_TO*]-(c:C)
    where r.r='9' 
    DETACH DELETE r,c

    MATCH (r:R)<-[:IS_A_C_BELONGING_TO*]-(c:C)
    where r.r='7' 
    DETACH DELETE r,c

enter image description here

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

使用级联的排序和集合:

MATCH (R:R)
OPTIONAL MATCH (R)<-[:IS_A_C_BELONGING_TO]-(C:C)
OPTIONAL MATCH (C)<-[:IS_A_S_BELONGING_TO_THAT_C]-(S:S)
WITH R, C, S 
     ORDER BY S.sName ASC
WITH R, C, 
     collect(S.sName) AS sNames 
     ORDER BY C.cName ASC
RETURN R.r AS rNames, 
       collect(C.cName) AS cNames,
       collect(sNames) AS sNames
       ORDER BY rNames ASC