Search code examples
ruby-on-railsneo4jneo4j.rb

How to neglect connected nodes in Neo4j.rb query


I have one node and one relationship

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

I want to find 2nd degree connected user's from User1 which is not connected with User1 yet

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

But this is giving me 0 results everytime I also tried with optional match but it was giving a huge number, Any help on this??


Solution

  • MATCH can't be used to find the lack of a pattern, that will never return rows. Using OPTIONAL MATCH instead should work.

    Or, if the where_not() method allows patterns, you could use:

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

    as an alternate means to exclude that relationship.