2013-05-21 48 views
1

我有一個暗號代碼是這樣的:暗號不要重複相同的結果

start n=node(*) 
match p=n-[r:OWES*1..200]->n 
return extract(s in relationships(p) : s.amount), 
     extract(t in nodes(p) : t.name), 
     length(p) 

查詢還給節點與關係連接的封閉的圈子欠下高達200級深。 給出的結果是:

2 

[155.55, 100641359] 

[LJUBOJEVIC STR I PRZIONA KAFE VL.LJ , SASA , LJUBOJEVIC STR I PRZIONA KAFE VL.LJ ] 


2 

[100641359, 155.55] 

[SASA , LJUBOJEVIC STR I PRZIONA KAFE VL.LJ , SASA ] 


3 

[100641359, 100641367, 550111.55] 

[SASA , LJUBOJEVIC STR I PRZIONA KAFE VL.LJ , ADVOKAT KOSTIC JEVREM VRBAS , SASA ] 

3 

[100641367, 550111.55, 100641359] 

[LJUBOJEVIC STR I PRZIONA KAFE VL.LJ , ADVOKAT KOSTIC JEVREM VRBAS , SASA , LJUBOJEVIC STR I PRZIONA KAFE VL.LJ ] 

3 

[550111.55, 100641359, 100641367] 

[ADVOKAT KOSTIC JEVREM VRBAS , SASA , LJUBOJEVIC STR I PRZIONA KAFE VL.LJ , ADVOKAT KOSTIC JEVREM VRBAS ] 

所以我讓我的結果返回次以上,如果是3級的關係,我得到3個結果,2我得到diferent順序2個相同的結果。如何改變我的暗號得到通過不在密碼中放棄*,只爲一條路徑產生一次結果。如果不是密碼,我可以用Java以某種方式處理。

回答

1

這是使用Cypher 2.0,因爲我正在使用STARTNODE功能。

這是一個怪胎,但它的作品。如果不添加一些嚴格的約束來保持整個集合的大小,我不會使用它。

CREATE 
    (a {name:'A'}), 
    (b {name:'B'}), 
    (c {name:'C'}), 
    (d {name:'D'}), 
    (e {name:'E'}), 
    (f {name:'F'}), 
    a-[:OWES {amount:100}]->b, 
    b-[:OWES {amount:200}]->c, 
    c-[:OWES {amount:300}]->a, 
    e-[:OWES {amount:400}]->f, 
    f-[:OWES {amount:500}]->e 



start nn=node(*) 
MATCH nn-[nr:OWES]->() 
WITH nn, nr ORDER BY nn.name, nr.amount 
WITH COLLECT([nn, nr.amount]) as sortedPairs 
START n=node(*) 
match p=n-[r:OWES*1..200]->n 
WITH sortedPairs, 
    extract(s in r: [STARTNODE(s), s.amount]) as pairs 
WITH 
    filter(sp in sortedPairs: ANY(f in pairs WHERE HEAD(f) = HEAD(sp) AND LAST(f) = LAST(sp))) as finalPairs  
return distinct 
    extract(s in finalPairs : HEAD(s)), 
    extract(s in finalPairs : LAST(s)), 
    length(finalPairs) 

結果: 詳細查詢結果 查詢結果

+----------------------------------------------------------------------------------------------------------------------+ 
| extract(s in finalPairs : HEAD(s))       | extract(s in finalPairs : LAST(s)) | length(finalPairs) | 
+----------------------------------------------------------------------------------------------------------------------+ 
| [Node[39]{name:"E"},Node[38]{name:"F"}]     | [400,500]       | 2     | 
| [Node[43]{name:"A"},Node[42]{name:"B"},Node[41]{name:"C"}] | [100,200,300]      | 3     | 
+----------------------------------------------------------------------------------------------------------------------+ 
2 rows 
13 ms 

Execution Plan 

Distinct(_rows=2, _db_hits=0) 
ColumnFilter(symKeys=["sortedPairs", "pairs", "finalPairs"], returnItemNames=["finalPairs"], _rows=5, _db_hits=0) 
    Extract(symKeys=["sortedPairs", "pairs"], exprKeys=["finalPairs"], _rows=5, _db_hits=0) 
    ColumnFilter(symKeys=["n", "sortedPairs", " UNNAMED155", "pairs", "p", "r"], returnItemNames=["sortedPairs", "pairs"], _rows=5, _db_hits=0) 
     Extract(symKeys=["n", "sortedPairs", " UNNAMED155", "p", "r"], exprKeys=["pairs"], _rows=5, _db_hits=13) 
     ExtractPath(name="p", patterns=[" UNNAMED155=n-[:OWES*1..200]->n"], _rows=5, _db_hits=0) 
      PatternMatch(g="(n)-[' UNNAMED155']-(n)", _rows=5, _db_hits=0) 
      AllNodes(identifier="n", _rows=6, _db_hits=6) 
       ColumnFilter(symKeys=[" INTERNAL_AGGREGATEfbdcf75a-046d-4501-9696-1e2c80469b29"], returnItemNames=["sortedPairs"], _rows=1, _db_hits=0) 
       EagerAggregation(keys=[], aggregates=["( INTERNAL_AGGREGATEfbdcf75a-046d-4501-9696-1e2c80469b29,Collect)"], _rows=1, _db_hits=5) 
        ColumnFilter(symKeys=["nr", " UNNAMEDS-2101388511", " UNNAMEDS2003458696", "nn", " UNNAMED39"], returnItemNames=["nn", "nr"], _rows=5, _db_hits=0) 
        Sort(descr=["SortItem(Cached( UNNAMEDS2003458696 of type Any),true)", "SortItem(Cached( UNNAMEDS-2101388511 of type Any),true)"], _rows=5, _db_hits=0) 
         Extract(symKeys=["nn", " UNNAMED39", "nr"], exprKeys=[" UNNAMEDS2003458696", " UNNAMEDS-2101388511"], _rows=5, _db_hits=10) 
         PatternMatch(g="(nn)-['nr']-( UNNAMED39)", _rows=5, _db_hits=0) 
          AllNodes(identifier="nn", _rows=6, _db_hits=6) 
+0

謝謝,我確實通過把兩個的ArrayList結果,刪除雙打一樣,......等等,你的解決方案是更優雅。謝謝! –