Skip to content

Instantly share code, notes, and snippets.

@chrisdkemper
Last active October 18, 2020 22:11
Show Gist options
  • Save chrisdkemper/794416dbae1bb17942b1 to your computer and use it in GitHub Desktop.
Save chrisdkemper/794416dbae1bb17942b1 to your computer and use it in GitHub Desktop.
This is an example Ecommerce data structure from the book Beginning Neo4j
//Add some contstraints for good measture, constraints must be ran individually
CREATE CONSTRAINT ON (c:Customer) ASSERT c.email IS UNIQUE;
CREATE CONSTRAINT ON (p:Product) ASSERT p.uuid IS UNIQUE;
//:Start Product and customer query:
//Products, bundles and categories
CREATE (product1:Product {name: "Product 1", uuid: "d8d177cc-1542-11e5-b60b-1697f925ec7b", price: 10})
CREATE (product2:Product {name: "Product 2", uuid: "d8d17b28-1542-11e5-b60b-1697f925ec7b", price: 20})
CREATE (product3:Product {name: "Product 3", uuid: "d8d17c72-1542-11e5-b60b-1697f925ec7b", price: 30})
CREATE (product4:Product {name: "Product 4", uuid: "d8d1b958-1542-11e5-b60b-1697f925ec7b", price: 40})
CREATE (product5:Product {name: "Product 5", uuid: "d8d1bade-1542-11e5-b60b-1697f925ec7b", price: 50})
CREATE (category1:Category {name: "Category 1"})
CREATE (category2:Category {name: "Category 2"})
CREATE (category3:Category {name: "Category 3"})
CREATE (bundle1:Bundle {name: "Bundle 1", price: 35})
//Customer creation
CREATE (customer1:Customer {name: "Chris", email: "hey@chrisdkemper.co.uk"})
CREATE (customer2:Customer {name: "Kane", email: "thebrother@chrisdkemper.co.uk"})
//Products in categories
CREATE UNIQUE (product1)-[:BELONGS_TO]->(category1)
CREATE UNIQUE (product2)-[:BELONGS_TO]->(category1)
CREATE UNIQUE (product3)-[:BELONGS_TO]->(category2)
CREATE UNIQUE (product4)-[:BELONGS_TO]->(category3)
CREATE UNIQUE (product5)-[:BELONGS_TO]->(category2)
//Products in bundle
CREATE UNIQUE (product1)-[:PART_OF]->(bundle1)
CREATE UNIQUE (product3)-[:PART_OF]->(bundle1)
//Nested category
CREATE UNIQUE (category3)-[:CHILD_OF]->(category1)
//:End Product and customer query:
//:Start Order query:
//Match the customer ready for the order
MATCH (customer:Customer {email: "hey@chrisdkemper.co.uk"})
,(product1:Product {uuid: "d8d177cc-1542-11e5-b60b-1697f925ec7b"})
,(product2:Product {uuid: "d8d17b28-1542-11e5-b60b-1697f925ec7b"})
//Create the order node
CREATE (order:Order {date: "2015-05-15"})
//Add the products to the order
CREATE UNIQUE (product1)-[:IN_ORDER]->(order)
CREATE UNIQUE (product2)-[:IN_ORDER]->(order)
//Relate the customer to the order
CREATE UNIQUE (customer)-[:CREATED]->(order)
//:End Order query:
//:Start Sale query:
//Add the sale node
CREATE UNIQUE (sale1:Sale {name: "Sale 1", active: TRUE})
//Products on sale
MATCH
(product4:Product {uuid: "d8d1b958-1542-11e5-b60b-1697f925ec7b"}),
(product5:Product {uuid: "d8d1bade-1542-11e5-b60b-1697f925ec7b"})
CREATE UNIQUE (product4)-[:ON_SALE {price: 36}]->(sale1)
CREATE UNIQUE (product5)-[:ON_SALE {price: 45}]->(sale1)
//:End Sale query:
@mbusch
Copy link

mbusch commented Jan 14, 2016

@chrisdkemper Just reading your recently released book due to research work for my master thesis. Although the Gist link was missing (maybe not the final version of your ebook on Springer?), I fortunately managed to find it.

There's a small typo included:

MATCH (product4:Product {uuid: "d8d1b958-1542-11e5-b60b-1697f925ec7b"),(product5:Product {uuid: "d8d1bade-1542-11e5-b60b-1697f925ec7b"}) --> MATCH (product4:Product {uuid: "d8d1b958-1542-11e5-b60b-1697f925ec7b"}),(product5:Product {uuid: "d8d1bade-1542-11e5-b60b-1697f925ec7b"}) (curly brace missing within first product)

Keep up the good work!

Edit: I found this to be a working Gist with v2.2.8 of Neo4j.

@chrisdkemper
Copy link
Author

@mbusch Thank you very much for your update, I've now put the correction in the main gist.

Also, the microsite is now online at chrisdkemper.co.uk/beginning-neo4j so any other updates that are brought to my attention will be referenced on there, or I'll update the original gist as I have here.

Thanks again for the correction! :)
Chris

@wilsonchua
Copy link

Thanks mbusch. Your "With Sales1" did the trick for me too!
Without that line, i get an error in the last portion.

@Makopo
Copy link

Makopo commented Dec 17, 2016

  • CREATE UNIQUE (sale1:Sale {name: "Sale 1", active: TRUE}) : UNIQUE is not required and it leads an error.
  • WITH sale1 is required just before MATCH in line 63.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment