Calculations of Nodes and Relationships

If we have 10000 calls, 2000 people and 5 cities, then our graph should have 12005 nodes. Given that there are 3 properties for each call, 3 for each person, and 1 for each city, we have a total of 36005 properties.

To calculate the total number of relationships, we can look into the db schema using command line 9 above. The schema is shown below. A person node has one relationship with the city and two (IN and OUT) relationships with the call. Given we have 10000 calls and 2000 people, we would have 10000×2 (IN and OUT) + 2000×1 (Person to City), giving us a total of 22000 relationships.

Cypher Basics

The main idea to understand is the concept of Graph Pattern Matching. There are no tables! Instead we an infinity plain surface and all the nodes are just placed on it. We have nodes and each one has a Node label, for example, (:City) or (:Person). These nodes are connected by relationships, in our case, [FROM], [IN], and [OUT].

How to find something?

To find something, we need to use matching patterns of these nodes and relationships. In Cypher we say, RETURN me data, that MATCH this pattern. In Cypher, there are no tables and no joins but nodes are connected by relationships.

Below are few basic commands:

  1. CREATE – to create data

  2. MATCH – specify a pattern of nodes and relationships

  3. WHERE – set filters to the results

  4. RETURN – request particular results

  5. DISTINCT – return unique results

  6. PROFILE or EXPLAIN – understand how your query works

  7. Use () parenthesis to indicate a node

  8. Use [] to indicate relationships

  9. CALL db.schema.visualization() – to visualise the schema of database

Node syntax
  • () – all types of nodes

  • (a) – all types of nodes via variable “a”

  • (:Label) – all nodes with Label

  • (c:City) – all nodes of type “City” via “c”

An example: MATCH (c:City) RETURN c

Relationship syntax
  • –> – relationship direction

  • [:REL_TYPE] – relationship of specified type

  • [:FROM]-> – directed relationship of type FROM

The below query is as follows: Search for the person Natalia and return the City she is from:

MATCH (p:Person)-[:FROM]->(c:City)
WHERE p.name = “Natalia”
RETURN p, c

Ryan

Ryan

Data Scientist

Leave a Reply