简体   繁体   English

如何在Neo4j数据库中的关系属性之一条件下编写Cypher查询?

[英]How to write a Cypher query with a condition on one of relationship properties in Neo4j database?

My question: 我的问题:

I am new to Neo4j and trying to create a query listing nodes and relationships into a graph with keyword as "id=0001" as below: 我是Neo4j的新手,并尝试创建一个查询,将节点和关系列出到图中,关键字为“ id = 0001” ,如下所示:

(a) - [id:'0001', ref_id:null] -> (b) - [id:'0002', ref_id:'0001'] -> (c) - [id:'0003', ref_id:'0002'] -> (d) (a)-[id:'0001',ref_id:null]->(b)-[id:'0002',ref_id:'0001']->(c)-[id:'0003',ref_id:' 0002']->(d)

Start Node will be (a) since it has relationship with id=0001 起始节点将为(a),因为它与id = 0001有关系

But the database also exists relationships which I don't want: 但是数据库也存在我不想要的关系:

(a) - [id:'2001', ref_id:null] -> (b) - [id:'2002', ref_id:'2001'] -> (c) (a)-[id:'2001',ref_id:null]->(b)-[id:'2002',ref_id:'2001']->(c)
(a) - [id:'3001', ref_id:null] -> (b) - [id:'3002', ref_id:'3001'] -> (c) (a)-[id:'3001',ref_id:null]->(b)-[id:'3002',ref_id:'3001']->(c)

The result should only includes: 结果应仅包括:

(a)-[0001]-(b)-[0002, 0001]-(c)-[0003,0002]-(d) (a)-[0001]-(b)-[0002,0001]-(c)-[0003,0002]-(d)

Below are what I was thinking before write question: 以下是我在写问题之前的思考:

I know how to create this query in SQL database like Oracle and MySQL, I can use query with "where" condition. 我知道如何在SQL数据库(如Oracle和MySQL)中创建此查询,我可以在“ where”条件下使用查询。 For example: 例如:

Select * 
from table_a parent, (select * from table_a) child 
where child.ref_id = parent.id

Then I can loop the result set in Java to find all relationships. 然后,我可以在Java中循环结果集以查找所有关系。 But this is stupid. 但这是愚蠢的。

I think the query should looks like: 我认为查询应如下所示:

Match (n)-[r:RELTYPE]->(m) WHERE {some conditions at here} RETURN n,r,m

Please help me, thank you! 请帮帮我,谢谢!

Yufan 雨帆

You could either use named relationships and filter in WHERE clause: 您可以使用命名关系并在WHERE子句中进行过滤:

match p = (a)-[r1:TYPE]->(b)-[r2:TYPE2]->(c)
where r1.id='0001' and r2.id='0002' and r2.ref_id='0001'
return p

Please note that properties having null value are not allowed in Neo4j. 请注意,Neo4j不允许使用具有空值的属性。 So the first relationship won't have a ref_id . 因此,第一个关系将没有ref_id

For the above notation is a shortcut by putting the conditions into the match : 对于上述表示法,是将条件放入match的捷径:

match p = (a)-[r1:TYPE {id:'0001'}]->(b)-[r2:TYPE2 {id:'0002', ref_id:'0001'}]->(c)
return p

On a side note : I'm not sure if the way you're using id and ref_id in relationship properties is a good way to model your data. 附带说明 :我不确定您在关系属性中使用idref_id的方式是否是对数据建模的好方法。 Maybe you can use more verbose relationship types - however without understanding the domain it's not really possible to give a good advice here. 也许您可以使用更多详细的关系类型-但是,如果不了解域,那么实际上不可能在这里给出好的建议。

I am using this Cypher query to find all neighbor with depth = 3. 我正在使用此Cypher查询来查找深度= 3的所有邻居。

MATCH (a)-[r1]-(b)-[r2]-(c)-[r3]-(n) 
WHERE n.APPLE_ID='12345' 
RETURN distinct n, distinct r3 

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM