简体   繁体   English

Neo4j gem - 查询不存在的关系

[英]Neo4j gem - Querying relationships that do not exist

I don't really have the code for this yet but I have the opposite of what I need 我还没有真正的代码,但我与我需要的相反

current_user.friends(:f).match_to(event)

This is going to return the friends that have a relationship to my event node. 这将返回与我的事件节点有关系的朋友。 What I need is the ones that don't have this relationship. 我需要的是那些没有这种关系的人。

Do I have to feed it something like in the docs 我是否必须像在文档中那样提供它

blog.query_as(:blog).match("blog<-[:COMMENTS_ON]-(:Comment)<-[:AUTHORED]-(author:Person)").where("author.age > 30").pluck(:author)

but nil the relationship? 但没关系?

so something like 所以像

current_user.friends.query_as(:f).match("event<-[invite:invited]-(f:User)).where(invite: nil)

Or is there another way? 或者还有另一种方式吗?

That query wouldn't work because if you specify a MATCH in cypher, you're saying that the relationship has to exist. 该查询不起作用,因为如果在cypher中指定MATCH,则表示该关系必须存在。 What you need to do is use the match ascii syntax in the WHERE clause (which neo4j begrudgingly supports for exactly this case): 你需要做的是使用WHERE子句中的匹配ascii语法(neo4j在这种情况下不情愿地支持):

current_user.friends.query_as(:friend).
  match(event: {neo_id: event.neo_id}).
  where("NOT(friend-[:invited]->(event:Event))").
  pluck(:friend)

It occurs to me that you should be able to do match(event: event) on that second line. 我觉得你应该能够在第二行做match(event: event) You might be able to, but I forget. 你可能会,但我忘记了。

Also note that the match_to applies to the most recent association in the chain (so in your first example you'd be trying to match the event object with one of the user's friends, which wouldn't work). 另请注意, match_to适用于链中最近的关联(因此在您的第一个示例中,您将尝试将event对象与用户的一个朋友匹配,这将无效)。

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

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