Skip to content

Instantly share code, notes, and snippets.

@cheerfulstoic
Created January 29, 2015 19:18
Show Gist options
  • Save cheerfulstoic/589ede139bfacdd8dfab to your computer and use it in GitHub Desktop.
Save cheerfulstoic/589ede139bfacdd8dfab to your computer and use it in GitHub Desktop.
store load script
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r;
CREATE (:Product {id: 1, name: 'Apple iPhone 6 128GB iOS 8 4.7" Touchscreen Factory Unlocked', price: 839.99});
CREATE (:Product {id: 2, name: 'HTC One M8 32GB Factory Unlocked Windows 8.1 Smartphone', price: 349.99});
CREATE (:Product {id: 3, name: 'DELL 15.6" Intel Core i7 1TB HDD Notebook', price: 649.99});
CREATE (:Product {id: 4, name: 'Canon EOS Rebel T5 EF-S 18-55mm IS II Digital SLR Kit US Warranty', price: 399.99});
CREATE (:Product {id: 5, name: 'Terralux TLF-TT-5 Tactical 5 Light 650 Lumens', price: 77.88});
CREATE (:Product {id: 6, name: 'Emergency One Person 3-month Food Storage Kit', price: 479.99});
CREATE
(electronics:Category {id: 7, name: 'Electronics'})-[:has_subcategory]->(:Category {id: 8, name: 'Phones'}),
electronics-[:has_subcategory]->(:Category {id: 9, name: 'Laptops'}),
electronics-[:has_subcategory]->(:Category {id: 10, name: 'Cameras'});
CREATE (:Category {id: 11, name: 'Home & Garden'})-[:has_subcategory]->(:Category {id: 12, name: 'Food & Beverage'});
CREATE (:Category {id: 13, name: 'Sporting Goods'})-[:has_subcategory]->(:Category {id: 14, name: 'Flashlights'});
MATCH (product:Product {id: 1}), (category:Category {id: 8})
CREATE product-[:has_category]->category;
MATCH (product:Product {id: 2}), (category:Category {id: 8})
CREATE product-[:has_category]->category;
MATCH (product:Product {id: 3}), (category:Category {id: 9})
CREATE product-[:has_category]->category;
MATCH (product:Product {id: 4}), (category:Category {id: 10})
CREATE product-[:has_category]->category;
MATCH (product:Product {id: 5}), (category:Category {id: 14})
CREATE product-[:has_category]->category;
MATCH (product:Product {id: 6}), (category:Category {id: 12})
CREATE product-[:has_category]->category;
CREATE (:User {name: 'Victor', age: 25});
CREATE (:User {name: 'Willie', age: 22});
CREATE (:User {name: 'Paula', age: 47});
CREATE (:User {name: 'Scott', age: 34});
CREATE (:User {name: 'Betty', age: 29});
MATCH (user1:User {name: 'Victor'}), (user2:User {name: 'Paula'})
CREATE user1-[:friend_of]->user2;
MATCH (user1:User {name: 'Betty'}), (user2:User {name: 'Scott'})
CREATE user1-[:friend_of]->user2;
MATCH (user1:User {name: 'Victor'}), (user2:User {name: 'Willie'})
CREATE user1-[:friend_of]->user2;
MATCH (user1:User {name: 'Willie'}), (user2:User {name: 'Paula'})
CREATE user1-[:friend_of]->user2;
MATCH (user1:User {name: 'Scott'}), (user2:User {name: 'Willie'})
CREATE user1-[:friend_of]->user2;
MATCH (user1:User {name: 'Betty'}), (user2:User {name: 'Paula'})
CREATE user1-[:friend_of]->user2;
MATCH (user:User {name: 'Victor'}), (product:Product)
WHERE product.id IN [1, 4, 6]
CREATE user-[:purchased {on: '2014-01-02 05:00'}]->product;
MATCH (user:User {name: 'Willie'}), (product:Product)
WHERE product.id IN [4, 5, 1]
CREATE user-[:purchased {on: '2014-01-04 11:00'}]->product;
MATCH (user:User {name: 'Paula'}), (product:Product)
WHERE product.id IN [5, 3]
CREATE user-[:purchased {on: '2014-01-03 07:00'}]->product;
MATCH (user:User {name: 'Scott'}), (product:Product)
WHERE product.id IN [4, 6, 1, 3]
CREATE user-[:purchased {on: '2014-01-07 20:00'}]->product;
MATCH (user:User {name: 'Betty'}), (product:Product)
WHERE product.id IN [3, 2]
CREATE user-[:purchased {on: '2014-01-03 15:00'}]->product;
# Show user friend relationships
MATCH (user:User) RETURN user
# Show products in category hierarchy
MATCH (product:Product)-[:has_category]->(category:Category)<-[:has_subcategory*]-(super_category:Category)
RETURN *
# All users 25 and under
MATCH (user:User) WHERE user.age <= 25 RETURN user.name, user.age;
# All products and their prices, sorted by price
MATCH (product:Product) RETURN product.name, product.price ORDER BY product.price DESC
# Get all purchases for all users and when they occurred
MATCH (user:User)-[purchase:purchased]->(product:Product) RETURN user.name, purchase.on, product.name;
# How much users spent in each sub category
MATCH (user:User)-[:purchased]->(product:Product)-[:has_category]->(category:Category)
RETURN user.name, sum(product.price), category.name
# How much users spent in all main categories
MATCH (user:User)-[:purchased]->(product:Product)-[:has_category]->(:Category)<-[:has_subcategory*]-(category:Category)
WHERE NOT(category<-[:has_subcategory]-(:Category))
RETURN user.name, sum(product.price), category.name
# How much was spent total in all main categories
MATCH (user:User)-[:purchased]->(product:Product)-[:has_category]->(:Category)<-[:has_subcategory*]-(category:Category)
WHERE NOT(category<-[:has_subcategory]-(:Category))
RETURN sum(product.price), category.name
# Average cost per purchase in all main categories
MATCH (user:User)-[:purchased]->(product:Product)-[:has_category]->(:Category)<-[:has_subcategory*]-(category:Category)
WHERE NOT(category<-[:has_subcategory]-(:Category))
RETURN sum(product.price) / count(product), category.name
# Things which friends have purchased
MATCH
(user1:User)-[:purchased]->(product:Product)<-[:purchased]-(user2:User),
user1-[:FRIEND_OF]-user2
RETURN user1.name, product.name, user2.name
# Count of number of times friends have purchased the same thing
MATCH
(user1:User)-[:purchased]->(product:Product)<-[:purchased]-(user2:User),
user1-[:FRIEND_OF]-user2
RETURN user1.name, product.name, count(user2)
ORDER BY count(user2) DESC
# Products purchased by users similar to Victor which Victor didn't purchase
MATCH (user:User {name: 'Victor'})-[:purchased]->(product:Product)<-[:purchased]-(most_similar_user:User)
WHERE ID(user) < ID(most_similar_user)
WITH user, most_similar_user, count(product) AS product_count
WHERE product_count >= 2
MATCH most_similar_user-[:purchased]->(product:Product)
WHERE NOT(user-[:purchased]->product)
RETURN product.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment