创建下一个节点不是前一个节点的路径?

时间:2017-08-16 23:54:15

标签: neo4j cypher

我在图表中有~1.5 M个节点,结构如下(图片)

Graph

我运行一个Cypher查询,对每个遍历的关系执行计算:

WITH 1 AS startVal
MATCH x = (c:Currency)-[r:Arb*2]->(m)
WITH x, REDUCE(s = startVal, e IN r | s * e.rate) AS endVal, startVal
RETURN EXTRACT(n IN NODES(x) | n) as Exchanges,
    extract ( e IN relationships(x) | startVal * e.rate) AS Rel,
    endVal, endVal - startVal AS Profit
ORDER BY Profit DESC LIMIT 5

问题是它返回路径("One")->("hop")->("One"),这对我来说毫无用处。

如何让它不选择以前走过的节点作为下一个节点(即"One"->"hop"->"any_other_node_but_not_"one")?

我已经读过NODE_RECENT应该解决我的问题。但是,没有关于如何在RestAPI或APOC过程中指定最近节点长度的示例。

我的案例是否有Cypher查询?

谢谢。

P.S。我对Neo4j和编码非常新(不到2个月)。如果有一个明显的简单解决方案,我很抱歉。

2 个答案:

答案 0 :(得分:1)

我不知道我是否完全理解了你的问题,但我相信你可以解决问题,在WHERE上添加MATCH条款,以防止不希望的关系匹配,像这样:

WITH 1 AS startVal
MATCH x = (c:Currency)-[r:Arb*2]->(m)
WHERE NOT (m)-[:Arb]->(c)
WITH x, REDUCE(s = startVal, e IN r | s * e.rate) AS endVal, startVal
RETURN EXTRACT(n IN NODES(x) | n) as Exchanges,
    extract ( e IN relationships(x) | startVal * e.rate) AS Rel,
    endVal, endVal - startVal AS Profit
ORDER BY Profit DESC LIMIT 5

答案 1 :(得分:0)

尝试在MATCH子句后插入此子句,以过滤cm相同的情况:

WHERE c <> m

[EDITED]

那是:

WITH 1 AS startVal
MATCH x = (c:Currency)-[r:Arb*2]->(m)
WHERE c <> m
WITH x, REDUCE(s = startVal, e IN r | s * e.rate) AS endVal, startVal
RETURN EXTRACT(n IN NODES(x) | n) as Exchanges,
    extract ( e IN relationships(x) | startVal * e.rate) AS Rel,
    endVal, endVal - startVal AS Profit
ORDER BY Profit DESC LIMIT 5;

使用此查询创建测试数据后:

CREATE
  (c:Currency {name: 'One'})-[:Arb {rate:1}]->(h:Account {name: 'hop'})-[:Arb {rate:2}]->(t:Currency {name: 'Two'}),
  (t)-[:Arb {rate:3}]->(h)-[:Arb {rate:4}]->(c)

以上查询产生以下结果:

+-----------------------------------------------------------------------------------------+
| Exchanges                                                     | Rel   | endVal | Profit |
+-----------------------------------------------------------------------------------------+
| [Node[8]{name:"Two"},Node[7]{name:"hop"},Node[6]{name:"One"}] | [3,4] | 12     | 11     |
| [Node[6]{name:"One"},Node[7]{name:"hop"},Node[8]{name:"Two"}] | [1,2] | 2      | 1      |
+-----------------------------------------------------------------------------------------+