Cypher查询以查找具有3个关系的节点 [英] Cypher query to find nodes that have 3 relationships
问题描述
当我寻找2个关系时,我想出了如何编写此查询,但不确定如何向查询中添加更多关系.
I figured out how to write this query when I am looking for 2 relationships, but not sure how to add more relationships to the query.
假设您有一个读书俱乐部数据库,其中读者"和书"为节点. 书"节点具有流派"属性(以定义书是小说,非小说,传记,参考书等).在读者"节点和书"节点之间存在已读"关系,其中有人读过一本书.
Assume you have a book club database with 'reader' and 'book' as nodes. The 'book' nodes have a 'genre' attribute (to define that the book is a Fiction, Non-Fiction, Biography, Reference, etc.) There is a Relationship "HasRead" between 'reader' nodes and 'book' nodes where someone has read a particular book.
如果我想查找同时阅读《小说》和《非小说》书籍的读者,则可以执行以下Cypher查询:
If I want to find readers that have read both Fiction AND Non-Fiction books, I could execute this Cypher query:
Start b1=node:MyBookIndex('Genre:Fiction'),
b2=node:MyBookIndex('Genre:Non-Fiction')
Match b1-[:HadRead]-r-[:HasRead]-b2
Return r.ReaderName
上述查询的关键是Match子句,该子句具有两个书名,这些书名将馈入"reader"节点的r
别名.
The key to the above query is the Match clause that has the two book aliases feeding into the r
alias for the 'reader' nodes.
问题:我该如何编写查询来查找已阅读小说 AND 非小说 AND 参考书的用户?当您要查找的东西超过2种时,我会陷入困境,如何写Match子句.
Question: How would I write the query to find users that have read Fiction AND Non-Fiction AND Reference books? I'm stuck with how you would write the Match clause when you have more than 2 things you are looking for.
推荐答案
您可以在单个MATCH
子句中指定多行,并用逗号分隔.例如,以下两个MATCH
子句在语义上是等效的(并且引擎将对其进行相同的评估):
You can have multiple line specified in a single MATCH
clause, separated by commas. For example, the following two MATCH
clauses are semantically equivalent (and will be evaluated identically by the engine):
//these mean the same thing!
match a--b--c
match a--b, b--c
您可以有任意数量的这些匹配项.因此,将其插入查询中,您将得到:
You can have any number of these matches. So, plugging that into your query, you get this:
start b1=node:MyBookIndex('Genre:Fiction'),
b2=node:MyBookIndex('Genre:Non-Fiction'),
b3=node:MyBookIndex('Genre:Reference')
match b1-[:HasRead]-r,
b2-[:HasRead]-r,
b3-[:HasRead]-r
return r.ReaderName
这篇关于Cypher查询以查找具有3个关系的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!