Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Created July 6, 2017 20:57
Neo4J Graph Days: notes

username/password: neo4j/neo4j1

building blocks

  • nodes = nouns
  • relationships = verbs
    • no dangling relationships
  • properties = adjectives or adverbs
    • on nodes or relationships
    • strings, floats, longs, arrays, etc.
    • not null
      • nodes either have or don't have the property
      • schemaless -> nodes have different numbers & types of properties
        • "heterogeneous or homogeneous as you'd like it to be"
          • implies you have to enforce homogeneity in app code
  • labels
    • handles for indexing
    • "group similar nodes into sub-graphs"
    • types, but not in a strict sense
    • 0-n labels per node

drawing graph patterns

nodes: ()

relationships: []

paths:

()-[]-()        <- we don't care about the direction
()-[]->()
()<-[]-()

Cypher basics:

MATCH (t: Terminal) 
RETURN t

= find all nodes labeled Terminal & return those

(cf. SQL: SELECT t.* from Terminals t)

MATCH p=(g:Gate)-[r:IN_TERMINAL]->(t:Terminal)
RETURN p

= find subgraphs of gates in terminals

MATCH (p:Place)-[:IN_CATEGORY]->(c:Category)
WHERE c.name = 'Barbecue'
RETURN p.name AS barbecue

= find names of places in categories named barbecue

Inline where clause:

MATCH (p:Place)-[:IN_CATEGORY]->(:Category {name:'Barbecue'})
RETURN p.name AS barbecue

(developer convenience, parsed as WHERE clause)

Aggregation (COUNT() etc. does exist; implicit group-by)

Outer joins: what if we want to return users with no friends?

MATCH (u.User)
OPTIONAL MATCH (u)-[:FRIENDS_WITH]-(f:User)
RETURN u.name AS user, COUNT(f) AS friends
ORDER BY friends desc

Queries on relationships w/o actually following them:

MATCH (u:User)
RETURN u.name AS user, SIZE((u)-[:FRIENDS_WITH]-()) AS friends
ORDER BY friends DESC

more efficient than

MATCH (u:User)-[:FRIENDS_WITH]-(f:User)

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