Learn about the Cypher query language

Tram Ho

You are probably familiar with database queries in the SQL language. SQL is a structured query language used for relational databases such as Mysql, PostgreSQL, …. However, NoSQL relational database management systems do not use this language. If you are learning graphing databases like Neo4j, Infinite graph, … start with Cypher.

In this article, I will introduce you to Cypher – graph database query language.

1. Basic Concepts

Cypher is Neo4j’s graph query language that allows users to store and retrieve data from a graph database. Cypher is used to query as well as update graphs (Graphs). Cypher is based on ASCII art, so its syntax is easy to understand and makes queries easier to understand. It focuses on clearly expressing what to retrieve from a chart, not on how to query it. Cypher Considered the easiest graph query language to learn.

1.1 Basic concepts of database Neo4j

Since Cypher is Neo4j’s query language, I will briefly introduce some of the basic concepts of Neo4j. For ease of understanding, I will compare these concepts with familiar concepts in RDBMS.

RDBMSNeo4j
TableGraph
RowNode
column and dataproperty and value
constrainrelationship
jointraversal

Neo4j stores data on nodes, constructing different dl structures based on relationships.

  • Button:
    • It is one of the two basic units that make up the graph.
    • Usually represent entities (depending on domain relationship).
    • Nodes and relationships can both contain attributes.
    • The simplest graph has only 1 single node.
  • Relationships:
    • A relationship that connects two nodes.
    • Relationships organize nodes into structures, allowing the chart to resemble a tree, list, map, or mixed entity.
    • A relationship can have attributes.
    • A two-node connection relationship is validated from start node to end node.
    • Relationship is always oriented, determined in the direction of entering or leaving a node => is the important factor used when browsing the graph.
    • A node can have a relationship with itself.
    • Relationship Type (relationship type): a relationship must have the exact type of relationship.
  • Properties:
    • Attributes are key-value pairs of which the primary key is a string.
    • The property does not contain null value, if a property has value = null, the property is deemed to not exist.
  • Label:
    • The name of a graph structure that groups nodes into a set (set).
    • Labels shape domains by grouping nodes into sets with certain names.
    • Labels can be added and deleted at runtime, used to mark the temporary status of the buttons. A button can have 0 or more labels.
  • Traversal: A way of querying graphs, navigating starting from a node to related nodes.

1.2 Basic Concepts of Cypher

Correlation between the basic query method of Cypher and SQL

1.3 Cypher Format

  • Format nodes (nodes)
    • () : the button is empty
    • (varname: NodeName) : The node is labeled NodeName, the variable name of the node is varname. The button may not have a variable name.
    • Clean query: Suggest to use CamelCase style label naming (first capital capital).
    • Should name a label reminiscent of the object, the button tag should be a noun.
    • Label names are case sensitive.
  • Relationship format (relationship)
    • [varname: RELATIONSHIP_NAME] : relationship is labeled RELATIONSHIP_NAME and the relational variable is varname.
    • Clean query: Suggest how to name labels in upper_case style, all capitalized and use hyphen between words. Relationship labels should be verbs. Label names are case sensitive.
  • Key properties, variables, parameters, aliases, and functions:
    • For example: title, size (), count (), firstName …
    • Case sensitive.
    • Clean query: should be written in camelCase format (the first letter is lowercase).
  • Clause:
    • For example: MATCH , WHERE , WITH , UNWIND
    • Clauses are case insensitive.
    • Clean queries: clauses should be styled as all capital letters, placed at the beginning of each line for easy reading and querying.
  • Keyword:
    • For example: AND , OR , IN , NOT , DISTINCT , STARTS WITH , CONTAINS , ENDS WITH , …
    • Keywords are not case sensitive.
    • Clean queries: Should be capitalized, no need to start a new line.
  • Line indents and line breaks
    • Each clause should be line break, in addition, subquery blocks, ON CREATE, ON MATCH should be line breaks and indented 2 spaces.
    • Use curly braces to group subquery blocks. If the subquery has only 1 line, there is no need to put it on a separate line or indent.
  • Metacharacter:
    • Single quotes: recommended for literal string values.
    • Example: ‘Mats ‘ quote: “statement” ‘, “Cypher’s a nice language”
  • Backticks
    • Used to avoid escaping characters with spaces or special characters.
    • Example: MATCH ( [email protected] $: Spaced Label { & property: 42})
  • Semi-colon
    • Used when there is a set of Cypher statements and need to separate commands. Not recommended when there is only 1 command.
  • Null and boolean values
    • Should be written in lower case in query.
    • For example: p.birthday = null, missingBirth = true
  • Handling samples
    • If a pattern is overflow, it should break the line after the arrow, not before.
    • For example:

    • Use anonymous nodes and relationships if not used later in the query.
    • For example:

    • Patterns should be concatenated to avoid variable repetition.
    • Named nodes or commonalities should be placed at the beginning of the MATCH clause. The sample relationships go (->) should be preceded by the sample relationships to (<-)
  • Space
    • There should be no spaces between the label predicate space. There are no spaces between samples.
    • A space on either side of the operator. A space after a comma.
    • Do not leave a space between the two parentheses.
    • Use a space between the subquery and the curly brace.

2. Basic Commands in Cypher

2.1 MATCH and RETURN

Example 1: Let label Person have 1 node with property {name: “Jennifer”}

Cypher command:

MATCH (p: Person {name: "Jennifer"}) RETURN p

Read more documentation about MATCH

2.2 Create data with Cypher

Example 2: Create a node labeled (label) Person with property {name: “Mark”}

Cypher command:

CREATE (p: Person {name: "Mark"}) RETURN p

Note: the return statement is optional. The newly created node will stand alone and have no relationship yet.

To establish relationships for it, we must use MATCH to avoid repeating the created node.

Cypher command:

Note: Without the above 2 MATCH commands, Cypher will automatically create new nodes without checking if it already exists in the database or not.

2.3 Update data with Cypher

Use MATCH … SET to add, edit, and delete properties in a button.

Example 1: Adding birthday attribute to Person Jennifer

Cypher command:

Example 2: Jennifer has been working at the company “Neo4j” since 2018

Cypher command:

2.4 Delete data with Cypher

Example 1: delete the friend relationship between Jennifer and Mark

Cypher command:

Example 2: delete the Person button that has the property {name: “Mark”}

Cypher command:

Example 3: Delete node and its relationship at the same time

Cypher command:

2.5 Delete property with Cypher

  • REMOVE: remove the attribute from the node completely and not store it any more.Example 1: Delete Jennifer birthday attribute
  • SET: Use the SET keyword to set the property value to null (in the csdl model Neo4j does not store null values).Example 2: Set the value of the birthday property to null

2.6 Merge in Cypher

Merge performs selectivity and checks whether data exists in the database or not, before inserting it into the database.

Example 1: Insert Person Mark into database by Merge

Cypher command:

The mark button already existed in the database before, so the above statement will not create a new mark button but only return an existing mark node.

Merge on a relationship: Use same as Create. If relationship has not been established, merge will perform a whole new creation (even though the node already exists).

Example 2: Make a relationship of friends between Jennifer and Mark

Cypher command:

Use MATCH to perform data matching before creating a relationship. This relationship has been created before so Merge only needs to return data that already exists.

Note: Using only MERGE without a data match will result in the MERGE command re-creating the created nodes if a relationship between those nodes cannot be found -> duplicate data.

2.7 Combine MERGE, CREATE, MATCH and SET

If we want to use MERGE to make sure not to create duplicates, also initialize some new properties or update other properties if it is only matched, in this case we use ON CREATE or ON MATCH with SET.

Cypher command:

2.8 WHERE Clause in Cypher

Compare the syntax using the previous query with the syntax using the WHERE:

Cypher command before:

Cypher command with WHERE:

Both queries return the same result. WHERE can do more than that.

  • WHERE NOT: Returns a property that does not match the pattern.Cypher command:

In addition, WHERE can also go with AND, OR, XOR.

Query in a certain range with WHERE:

  • WHERE exists (property): check if an attribute exists in the node or a relationship exists in the pattern.Cypher command:
  • WHERE … IN: Checks if the attribute value is the value in the given list.Cypher command:

2.9 Cypher handles string strings

STARTS WITH: find the string that starts with the string you specified.


CONTAINS: checks if the specified string is part of the attribute value.


ENDS WITH: find a string that ends with the string you specified.


Regular expressions: check a value of a string with regular expressions.

Example: Find all Person nodes that begin with “Jo”.

Cypher command:

2.10 Optional patterns in Cypher

Returns results even if they do not match all of the sample or criteria. It is similar to outer join in SQL.

  • OPTIONAL MATCH: if no result is found, the matched row will return null.Example: Find people whose names start with ‘J’ and work in a company.

The Cypher command above will return all people whose names start with ‘J’, and the name of the company they’re working for. Those who do not work at the company, the company name they work with will return null.

2.11 Complex patterns

Example: Find people who also like graph technology in addition to Jennifer and they are Jennifer’s friends.

The above example uses commas to concatenate patterns, commas allow stringing patterns together (similar to WHERE exists (pattern)). However this structure can add many different templates more flexibly in the graph.


Above is an overview article about Cypher as well as the commands to query the basic Cypher that I have synthesized. Hopefully this article will help you in your first step to learn this new query language. Wish you have a productive day

References

1. Cypher query language – for beginners

2. Overview of SQL

Share the news now

Source : Viblo