Skip to content

Instantly share code, notes, and snippets.

@wjgilmore
Last active November 10, 2020 09:04
Show Gist options
  • Save wjgilmore/8ba5f31ef1435dc04c52 to your computer and use it in GitHub Desktop.
Save wjgilmore/8ba5f31ef1435dc04c52 to your computer and use it in GitHub Desktop.
Neo4j query examples
#######
# GENERAL NOTES
#######
* Node labels are case-sensitive. Customer and customer are different!
* Relationship names are case-sensitive. OWNS and owns are different!
* You can create nodes/relationships iff they don't exist using UNIQUE: http://docs.neo4j.org/chunked/snapshot/query-create-unique.html.
#######
# ADMIN QUERIES
#######
Return distinct list of node labels
START n=node(*) RETURN distinct labels(n);
#######
# QUERIES
#######
# Delete everything
MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r
This can get buggy when dealing with larger databases because everything must
be loaded before it can be deleted. If you're just testing and want to throw
truly everything away then just delete the neo4j/data/graph.db directory and restart the Neo4j server.
# Delete all relationships
START r = relationship(*) DELETE r;
# How many total nodes?
start n=node(*) return count(n);
# Find a specific node by id
start n=node(4245) return n;
# Give me all customers
match (n:Customer) return n as Customer
# Give me the labels associated with a node
MATCH (customer {email:'john@example.com' }) return labels(customer)
# Give me all customers who own at least one book.
match (n:Customer)-[:owns]->(books) return n as Customer
Note the above query does not return a distinct list of customers. To return a distinct list, use this:
match (n:Customer)-[:owns]->(books)
return distinct n as Customer
# How many customers are there?
match (n:Customer) return count(n)
# Find a specific customer by name
match (n:Customer) where n.name="John Smith" return n
# Find all customers living in the United States
match (customer:Customer)
where customer.country = "United States"
return customer
# Find all distinct customers living in the United States
# who have purchased at least one book
match (customer:Customer)-[:owns]->(books)
where customer.country = "United States"
return distinct customer
# Return all of the book titles associated with customer John Smith
MATCH (customer { name:'John Smith' })-->(book) RETURN book.title
## Another way to accomplish same thing
start
p=node:node_auto_index(name='John Smith')
match (p)-[:owns]->(book)
return book.pisbn
limit 5
## Order titles according to purchase date
start
p=node:node_auto_index(name='John Smith')
match (p)-[r:owns]->(book)
return book.pisbn order by r.since desc
limit 5
#####
# RELATIONSHIPS
#####
# How many total relationships are there?
START r=relationship(*) RETURN count(r)
# List relationships by type
MATCH (a)-[r]->(b)
RETURN DISTINCT head(labels(a)) AS This, type(r) as To, head(labels(b)) AS That
LIMIT 10
# Count a certain type of relationship
match (c:Customer)-[:views]->(books) return count(c)
# Return all customers who own the book titled A
MATCH (book { title:'A' })<-[:owns]-(customer) RETURN customer
# How many of each book do customers own?
start n=node:node_auto_index(type='Customer')
match (n)-[:owns]->(o) return o.title, count(*) order by count(*) desc
# How many customers who bought "A" also bought "B"?
start
n=node:node_auto_index(title='A'),
m=node:node_auto_index(title='B')
match (p)-[:owns]->(n), (p)-[:owns]->(m)
return count(*)
Another way to do the above, I wonder which one is more efficient:
start
n=node:node_auto_index(pisbn="9781430240952")
match (p)-[:owns]->(n), (p)-[:owns]->(book {pisbn:'9781430242369' })
return distinct p.name
limit 5
# Sales correlations. Given a set of customers C1 who own book p, give me me a
# set of customers C2 who own a book also owned by a customer
# found in C1 but who do not own book p:
match
(p:Book {pisbn:"9781430242369"})<-[:owns]-(peer:Customer)
-[:owns]->(other:Book)<-[o:owns]-(target:Customer)
WHERE NOT( (target)-[:owns]->(p))
AND o.since > "2013-10-01"
return target.name, o.since
limit 10;
# What five books are most commonly purchased in conjunction
# with the book having ISBN 123456?
start
n=node:node_auto_index(pisbn="123456")
match (p)-[:owns]->(n), (p)-[:owns]->(book)
return book.title, count(*) order by count(*) desc
limit 5
# Does a customer own a specific book?
START n1=node:node_auto_index(name="John Smith"), n2=node:node_auto_index(pisbn="123456")
MATCH n1-[:OWNS]->n2
RETURN count(*)
########
# INDEXES
########
# Create Index
create index on :Book(pisbn)
########
# NEOGRAPHY
########
## Embellish a Relation
@neo.create_relationship("owns", node1, node2, {"since" => order.order_date})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment