如何编写Cypher查询来检查节点之间的可交换性

时间:2016-06-10 21:13:05

标签: neo4j cypher

我需要检查节点之间的交换性。整个节点有3个关系,跟踪如下:aabbca,aabcba,aacbba。现在可以清楚地看到,每个b和c节点放在一起,他们通勤。下面我使用查询我如何生成我的图表。

这是我的图表:

CREATE (x1:Node {title:'a'})

CREATE (x2:Node {title:'a'})

CREATE (x3:Node {title:'b'})

CREATE (x4:Node {title:'b'})

CREATE (x5:Node {title:'c'})

CREATE (x6:Node {title:'a'})

CREATE (x1) - [:transition {id:[1]}] -> (x2)

CREATE (x2) - [:transition {id:[1]}] -> (x3)

CREATE (x3) - [:transition {id:[1]}] -> (x4)

CREATE (x4) - [:transition {id:[1]}] -> (x5)

CREATE (x5) - [:transition {id:[1]}] -> (x6)

CREATE (x1) - [:transition {id:[2]}] -> (x2)

CREATE (x2) - [:transition {id:[2]}] -> (x3)

CREATE (x3) - [:transition {id:[2]}] -> (x5)

CREATE (x5) - [:transition {id:[2]}] -> (x4)

CREATE (x4) - [:transition {id:[2]}] -> (x6)

CREATE (x1) - [:transition {id:[3]}] -> (x2)

CREATE (x2) - [:transition {id:[3]}] -> (x5)

CREATE (x5) - [:transition {id:[3]}] -> (x3)

CREATE (x3) - [:transition {id:[3]}] -> (x4)

CREATE (x4) - [:transition {id:[3]}] -> (x6)

产生类似这样的东西:

enter image description here

现在为了清楚地识别交换节点,我继续在关系id 1&的基础上进行过滤。 2,1& 3和2& 3使用以下查询我得到相应的图表:

MATCH (a)-[r]->(b) WHERE 1 IN r.id OR 2 In r.id  RETURN a,r,b 
MATCH (a)-[r]->(b) WHERE 1 IN r.id OR 3 In r.id  RETURN a,r,b
MATCH (a)-[r]->(b) WHERE 2 IN r.id OR 3 In r.id RETURN a,r,b

案例1: enter image description here 案例2 :: enter image description here 案例3: enter image description here

案例2 的圆圈部分,对于我们的示例只有2个节点,但在通用情况下,它可以有1个(就像我们在案例1中看到的那样& 案例3 到N个节点,其中N是有限数。

现在已经说过,我想创建一个cypher查询,它将检查图中是否存在这样的可通信节点,并且基于我需要使用类似的查询从图中获取节点和关系这样:

MATCH (a)-[r]->(M)-[r]->(K)-[r]->[b], 
(b)-[s]->(K)-[s]->(M)-[s]->(a)
WHERE r.id <> s.id 
AND M is returning minimum 1 node
AND K is returning minimum 1 node
RETURN a,r,M,K,s,b

K = M 

M = MATCH (O)-[r]->(b1), (N)-[r]->(P)->[s]->(N)
WHERE  
(b1 = b
RETURN b)
OR 
(N = M AND P = M
AND N is returning at least 1 node
AND P is returning at least 1 node 
RETURN N,r,P,s)

此时我可能没有明确定义我的约束,但我需要帮助的是正确表示上述查询。有人可以帮忙吗?

更新: 我刚刚解决了案例1和案例案例3(基本上是相同类型的条件),但这不是通用的解决方案。在通用解决方案中,案例2也会起作用。:

MATCH (a)-[r]->(b)-[s]->(c)-[t]->(d)
WHERE (1 IN r.id OR 2 IN r.id) 
AND (1 IN s.id OR 2 IN s.id) 
AND (1 IN t.id OR 2 IN t.id)
WITH a,r,b,s,c,t,d
MATCH (a)-[r1]->(c)-[s1]->(b)-[t1]->(d)
WHERE ( r <> r1 AND (1 IN r1.id OR 2 IN r1.id)) AND
(s<>s1 AND (1 IN s1.id OR 2 IN s1.id)) AND
(t<>t1 AND (1 IN t1.id OR 2 IN t1.id)) 
RETURN a,r,r1,b,s,s1,c,t,t1,d

MATCH (a)-[r]->(b)-[s]->(c)-[t]->(d)
WHERE (3 IN r.id OR 2 IN r.id) 
AND (3 IN s.id OR 2 IN s.id) 
AND (3 IN t.id OR 2 IN t.id)
WITH a,r,b,s,c,t,d
MATCH (a)-[r1]->(c)-[s1]->(b)-[t1]->(d)
WHERE ( r <> r1 AND (3 IN r1.id OR 2 IN r1.id)) AND
(s<>s1 AND (3 IN s1.id OR 2 IN s1.id)) AND
(t<>t1 AND (3 IN t1.id OR 2 IN t1.id)) 
RETURN a,r,r1,b,s,s1,c,t,t1,d

1 个答案:

答案 0 :(得分:1)

Meteor