如何使用类型为List的关系属性过滤Neo4j中的节点

时间:2018-11-19 10:27:25

标签: neo4j

如何检索特定用户的所有影响者并基于特定属性(例如在以下示例中定义的关系)中,我想在prid ='c1'中检索p1的所有影响者并仅在PR1中投影?

样本数据集

 CREATE (p1:People {name:'P1 User',  id: 'p001'})
CREATE (p2:People {name:'P2 User',  id: 'p002'})
CREATE (p3:People {name:'P3 User',  id: 'p003'})
CREATE (p4:People {name:'P4 User',  id: 'p004'})
CREATE (p5:People {name:'P5 User',  id: 'p005'})

CREATE
      (p1)<-[:INFLUENCER {prid:'c1', projects:['PR1','PR2']}]-(p2),
      (p1)<-[:INFLUENCER {prid:'c1', projects:['PR1','PR4']}]-(p3),
      (p1)<-[:INFLUENCER {prid:'c1', projects:['PR1']}]-(p4),
      (p2)<-[:INFLUENCER {prid:'c1', projects:['PR1']}]-(p3),
      (p3)<-[:INFLUENCER {prid:'c1', projects:['PR4','PR5']}]-(p4),
      (p4)<-[:INFLUENCER {prid:'c1', projects:['PR4','PR5']}]-(p5)

 MATCH (p:People{gsid:'p001'})<-[r:INFLUNCED_BY]-(:People)
    WHERE r.prid='c1' AND r.projects IN ['PR1']
    RETURN p,r;

即使存在以下关系,上述查询也不会返回记录

MATCH (node) RETURN node;

The result for the above query

1 个答案:

答案 0 :(得分:1)

首先,您在查询中的节点属性和关系类型没有相同的名称:

gsid vs id
INFLUNCED_BY vs INFLUENCER

第二,您需要检查列表的交集,而不是事件的出现。所以根据测试数据:

MATCH (p:People {id:'p001'})<-[r:INFLUENCER]-(:People)
WHERE r.prid ='c1' AND 
      // Check the lists for intersection
      any(p IN r.projects WHERE p IN ['PR1'])
RETURN p, r;