如何在节点关系上编写以下Cypher查询?

时间:2016-02-14 11:34:12

标签: neo4j cypher neo4jclient

我有

下的示例数据库
CREATE (Prod1:TestProdNeoForJ {productname:'Product1'})
CREATE (A:Person {name:'A S'})
CREATE (R:Person {name:'R B'})
CREATE (L:Person {name:'L R P'})
CREATE (P:Person {name:'P R'})
CREATE (G:Person {name:'G K'})
CREATE (RK:Person {name:'R K'})


CREATE  
    (A)-[:WORKS_IN {roles:['Technical Lead']}]->(Prod1),    
    (R)-[:WORKS_IN {roles:['Technical Lead']}]->(Prod1),    
    (L)-[:WORKS_IN {roles:['Senior Developer']}]->(Prod1),
    (P)-[:WORKS_IN {roles:['Junior Project Manager']}]->(Prod1),    
    (G)-[:WORKS_IN {roles:['Senior Project Manager']}]->(Prod1),
    (A)-[:REPORTS_TO {roles:['Technical Lead']}]->(G),
    (R)-[:REPORTS_TO {roles:['Technical Lead']}]->(G),
    (P)-[:REPORTS_TO {roles:['Junior Project Manager']}]->(G),
    (G)-[:REPORTS_TO {roles:['Senior Project Manager']}]->(RK),
    (RK)-[:DH]->(Prod1)

enter image description here

我正在寻找以下关于关系的密码查询

a)人们向'G'报告

(答案是:A,R,P)

b)向“G”报告其角色为“技术主管”的人

(答案是:A,R)

c)人们直接向'RK'报告

(答案是:G)

d)人民直接/间接向“RK”报告

(答案将是:G,P,R,A)因为A,R,P报告G和G直接向RK报告

到目前为止我尝试过的(第一次):

var query = WebApiConfig.GraphClient.Cypher
          .Match("(m:TestProdNeoForJ)")
          .OptionalMatch("(a:Person)-[r1:REPORTS_TO]->(b:Person)")
          .Return((a, b) => new
          {                  
              cast = Return.As<string>("collect(a.name)")
          })
          .Limit(100);
var data = query.Results.ToList();

密码正在产生结果(当然因为我必须限制直到G)。

[
  "A S",
  "R B",
  "P R",
  "G K"
]

1 个答案:

答案 0 :(得分:1)

A)

MATCH (p:Person)-[:REPORTS_TO]->(:Person{name:'G K'}) RETURN p

b)中

MATCH (p:Person)-[r:REPORTS_TO]->(:Person{name:'G K'})
WHERE 'Technical Lead' IN r.roles 
RETURN p

由于您已选择对角色使用数组属性,因此您需要使用IN检查“技术主管”是否属于该数组的一部分。根据用例,将角色建模为单独的节点可能是有意义的。

c)

MATCH (p:Person)-[:REPORTS_TO]->(:Person{name:'R K'}) RETURN p

基本上与a)相同。

d)

MATCH (p:Person)-[:REPORTS_TO*]->(:Person{name:'R K'}) RETURN p

这使用可变路径长度表达式*