Neo4j - How to display a graph of Nodes connected to properties of related nodes

140 Views Asked by At

I have Nodes Customer and Product - with a directed relationship TRANSACTION from Customer to Product. Product has the property category.

Is it possible to display all the Customers connected to all the categories which they have bought? As though the categories were a node rather than a property.

1

There are 1 best solutions below

3
Christophe Willemsen On BEST ANSWER

You can use APOC Virtual Nodes and Relationships to display such graph :

MATCH (n:Product)<-[:TRANSACTION]-(c:Customer)
WITH n.category AS category, c, count(*) AS numberOfPurchases
WITH 
apoc.create.vNode(['Category'], {name: category}) AS catNode,
c,
numberOfPurchases
RETURN catNode, c, 
apoc.create.vRelationship(c, 'PURCHASED_IN_CATEGORY', {amount: numberOfPurchases}, catNode) AS rel