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)

该查询返回与关系 OWES 连接的封闭圆圈中的节点,深度可达 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 我会以不同的顺序得到 2 个相同的结果。如何通过不放弃 * in 来更改我的密码以仅在一条路径上获得一次结果一个密码。如果不是在密码中,我可以在 Java 中以某种方式处理这个问题。

4

1 回答 1

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)
于 2013-08-09T15:17:30.050 回答