Nodes and relationships

Cypher queries include patterns that describe nodes and relationships. The syntax specifies whether a part of a query is a node or a relationship.

Specifically, in the query below, n and m are nodes, surrounded by (). The relationship is represented by . Values like m and n are parameters, which can be passed to later clauses.

MATCH p=(n)–(m) RETURN p

To add details to the relationship, brackets [] should be added. To specify the direction of a relationship, arrowheads > or < can also be included. In this case, the query finds a node n that is part of m.

MATCH p=(n)-[x]->(m) RETURN p

Nodes have labels and relationships have types. These are indicated after : and can be preceded by a parameter, but this is not necessary if whole patterns including the node or relationship are returned, rather than specific nodes or relationships. Node labels especially can make it much faster to run Cypher queries, since they drastically reduce the number of nodes that needs to be matched to the pattern.

Note that the Cypher style guide recommends writing relationship types in upper-case with an underscore between words, as is shown below.

MATCH (n:Specimen)-[x:PART_OF]->(m:Experiment) RETURN x
MATCH p=(:Specimen)-[:PART_OF]->(:Experiment) RETURN p

In addition to labels and types, nodes and relationships can have properties. The syntax for accessing properties is identical across nodes and relationships. The queries below find an experiment linked a sample with a pH of 8 and a taxon with a Spearman correlation of 0.6 that is linked to a pH node.

MATCH (n:Specimen {name: “Sample 1”, pH: 8})-[x:PART_OF]->(m:Experiment) RETURN m
MATCH (n:Taxon)-[x:SPEARMAN_CORRELATION {value: 0.6}]->(:Property {name: “pH”}) RETURN n

Properties can be specified in multiple ways: one is to include them inside the node or relationship in {} brackets, as shown above. The other is to specify the node or relationship as a parameter and refer to the property after it. The latter is shown below; this query finds any taxa with a positive Spearman correlation to pH.

MATCH (n:Taxon)-[x:SPEARMAN_CORRELATION]->(:Property {name: “pH”}) WHERE x.value > 0 RETURN n