Cypher do not repeat same results

2019-07-29 10:56发布

I have a cypher code that goes like this :

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)

The query gives back nodes in a closed circle connected with relation OWES up to 200 level deep . The results given are :

   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 ]

So I get my results returning more times , if it is 3 relations level I get 3 results , 2 I get 2 same results in diferent order .How to change my cypher to get result only once for one path by not giving up from * in a cypher . If not in cypher can I handle this some way in Java .

1条回答
成全新的幸福
2楼-- · 2019-07-29 11:21

This is using Cypher 2.0 because I'm making use of the STARTNODE function.

It is a bit of a monstrosity, but it works. I wouldn't use it without adding some serious constraints to keep the overall collection size down.

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)

Results: Detailed Query Results Query Results

+----------------------------------------------------------------------------------------------------------------------+
| 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)
查看更多
登录 后发表回答