【问题标题】:How to neglect connected nodes in Neo4j.rb query如何忽略 Neo4j.rb 查询中的连接节点
【发布时间】:2017-04-24 19:56:08
【问题描述】:

我有一个节点和一个关系

class User
  include Neo4j::ActiveNode

  property :first_name
end


class Connection
  include Neo4j::ActiveRel
  include Enumable

  creates_unique

  from_class 'User'
  to_class 'User'
  type 'connected_to'

  property :status, type: Integer, default: 0
end

我想从 User1 中找到尚未与 User1 连接的二级连接用户

User.find(1).query_as(:s)
  .match('(s) - [r1 :connected_to] - (mutual_friend) 
    - [r2 :connected_to] - (friends_of_friend: `User`)')
  .match('(s)-[r4:connected_to]-[friends_of_friend]')
  .where('r1.status = 1 AND r2.status = 1 AND r4 IS NULL')
  .pluck('DISTINCT friends_of_friend.uuid').count

但是每次我也尝试使用可选匹配时,这给了我 0 个结果,但它给出了一个巨大的数字,对此有什么帮助吗??

【问题讨论】:

    标签: ruby-on-rails neo4j neo4j.rb


    【解决方案1】:

    InverseFalcon 是对的,尽管您可以做很多其他的事情来让它变得更简单:

    class User
      include Neo4j::ActiveNode
    
      property :first_name
    
      has_many :both, :connected_users, type: :connected_to, model_class: :User
    end
    
    
    # ActiveRel isn't strictly needed for this,
    # though to have the `default` or any other logic it's good to have it
    
    
    user = User.find(1)
    
    user.as(:user)
        .connected_users.rel_where(status: 1)
        .connected_users(:friend_of_friend).rel_where(status: 1)
        .where_not('(user)-[:connected_to]-(friend_of_friend)')
        .count(:distinct)
    

    我认为这也可以:

    user.as(:user)
        .connected_users(:friend_of_friend, nil, rel_length: 2).rel_where(status: 1)
        .where_not('(user)-[:connected_to]-(friend_of_friend)')
        .count(:distinct)
    

    【讨论】:

    • 谢谢,太棒了,很有帮助
    【解决方案2】:

    MATCH 不能用于查找模式的缺失,它永远不会返回行。改用 OPTIONAL MATCH 应该可以。

    或者,如果where_not() 方法允许模式,您可以使用:

     .where_not('(s)-[:connected_to]-(friends_of_friend)')
    

    作为排除这种关系的替代方法。

    【讨论】:

    • 感谢@brian & inverseFalcon
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2019-08-23
    • 2017-12-28
    相关资源
    最近更新 更多