Reputation: 23472
I'm working on a simple demo in neo4j where I want to use recommendations based on orders and how has bought what. I've created a graph here: http://console.neo4j.org/?id=jvqr95.
Basically I have many relations like:
(o:Order)-[:INCLUDES]->(p:Product)
An order can have multiple products.
Given a specific product id I would like to find other products that was in an order containing a product with the given product id and I would like to order it by the number of orders the product is in.
I've tried the following:
MATCH (p:Product)--(o)-[:INCLUDES]->(p2:Product)--(o2)
WHERE p.name = "chocolate"
RETURN p2, COUNT(DISTINCT o2)
but that doesn't give me the result I want. For that query I expected to get chips
back with a count of 2, but I only get a count of 1.
And for the follwing query:
MATCH (p:Product)--(o)-[:INCLUDES]->(p2:Product)--(o2)
WHERE p.name = "chips"
RETURN p2, COUNT(DISTINCT o2)
I expect to get chocolate
and ball
back where each has a count of 1, but I don't get anything back. What have I missed?
Upvotes: 2
Views: 87
Reputation: 7790
You're matching on too many things in your initial MATCH
.
MATCH (o:Order)-[:INCLUDES]->(p:Product { name:'ball' })
MATCH (o)-[:INCLUDES]->(p2:Product)
WHERE p2 <> p
MATCH (o2:Order)-[:INCLUDES]->(p2)
RETURN p2.name AS Product, COUNT(o2) AS Count
ORDER BY Count DESC
In English: "Match on orders that include a specific product. For these orders, get all included products that are not the initial product. For these products, match on orders that they are included in. Return the product name and the count of how many orders it's been included in."
http://console.neo4j.org/?id=q49sx6
http://console.neo4j.org/?id=uy3t9e
Upvotes: 4