Peter Whitfield
Peter Whitfield

Reputation: 525

using cypher to find nodes in a subgraph that are NOT connected to a specified node

I'm learning cypher with Neo4j but I'm having some problems that show I still don't quite get it.

I'm trying to write a query that finds a subgraph and then excludes nodes from that subgraph that are connected to a specified node.

In practice, it's a recommendation problem: I find a set of recommendations, but want to exclude those things that a target use already knows about.

I thought I could do something like:

match (u:User{id:"some id"}), (:Category{title:"some category"})-[:categorizes]->(i:Item) 
where not (u)-[:knows_about]-(i) 
return  i

but this doesn't work.

Can anyone explain what I'm doing wrong/what I should be doing?

Upvotes: 3

Views: 735

Answers (1)

tstorms
tstorms

Reputation: 5001

I think you want the following:

MATCH (:Category{title:"some category"})-[:categorizes]->(i:Item)
MATCH (u:User {id:some_id})
WHERE  not (u)-[:knows_about]-(i) 
RETURN i

You might want to add a direction in the second WHERE clause (performance!).

Upvotes: 2

Related Questions