【问题标题】:cypher find relation direction密码找到关系方向
【发布时间】:2013-07-17 15:23:16
【问题描述】:

如何找到与包含路径相关的关系方向?我需要这个来进行考虑关系方向的加权图搜索(用0 衡量“错误”方向,另请参见 cmets)。

让我们说:

START a=node({param})
MATCH a-[*]-b
WITH a, b
MATCH p = allshortestpaths(a-[*]-b)
RETURN extract(r in rels(p): flows_with_path(r)) as in_flow

在哪里 flows_with_path = 1 如果sp = (a)-[*0..]-[r]->[*0..]-(b),否则 0

编辑:更正查询

【问题讨论】:

  • 如果 sp 包含传入和传出关系会发生什么?
  • in_flow 然后会指出这一点。例如。 [1, 0, 0, 1, 1] 表示 a 和 b 之间有 5 个 rel,rel 0, 3, 4 是传出的。也可能在路径中存在传入和传出关系否则相同。我想它应该说 allshortestpaths 来澄清这一点。在这种情况下,例如在位置 2 有 2 个关系,我可能会得到 2 个向量,[1, 0, 0, 1, 1] 和 [1, 1, 0, 1, 1]

标签: neo4j cypher


【解决方案1】:

所以,这是一种使用现有密码函数的方法。我不保证它的性能超级好,但试一试。我们正在使用 reduce 构建我们的集合,使用带有集合的累加器元组和我们查看的最后一个节点,因此我们可以检查它是否连接到下一个节点。这需要 2.0 的 case/when 语法——在 1.9 中可能有办法做到这一点,但它可能更复杂。

START a=node:node_auto_index(name="Trinity") 
MATCH a-[*]-b 
WHERE a <> b
WITH distinct a,b 
MATCH p = allshortestpaths(a-[*]-b) 
RETURN extract(x in nodes(p): x.name?), // a concise representation of the path we're checking
  head(
    reduce(acc=[[], head(nodes(p))], x IN tail(nodes(p)): // pop the first node off, traverse the tail
      CASE WHEN ALL (y IN tail(acc) WHERE y-->x)  // a bit of a hack because tail(acc)-->x doesn't parse right, so I had to wrap it so I can have a bare identifier in the pattern predicate
           THEN [head(acc) + 0, x]    // add a 0 to our accumulator collection
           ELSE [head(acc) + 1, x]    // add a 1 to our accumulator collection 
      END )) AS in_line

http://console.neo4j.org/r/v0jx03

输出:

+---------------------------------------------------------------------------+
| extract(x in nodes(p): x.name?)                               | in_line   |
+---------------------------------------------------------------------------+
| ["Trinity","Morpheus"]                                        | [1]       |
| ["Trinity","Morpheus","Cypher"]                               | [1,0]     |
| ["Trinity","Morpheus","Cypher","Agent Smith"]                 | [1,0,0]   |
| ["Trinity","Morpheus","Cypher","Agent Smith","The Architect"] | [1,0,0,0] |
| ["Trinity","Neo"]                                             | [1]       |
| ["Trinity","Neo",<null>]                                      | [1,1]     |
+---------------------------------------------------------------------------+

注意:感谢@boggle 的头脑风暴会议。

【讨论】:

  • 所以 [head(acc) + 0, x] 中的 + 将元素附加到数组 - 相当于 [head(acc) + [0], x],对吧?
  • 我必须学习 cypher 才能得到这个,但现在我可以确认你保持你的状态 ;) 但开个玩笑,我认为如果你只是发布一个那里有几个更高级的查询。我从他们每个人身上学到了很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多