Graph database NEO4J create, insert, delete, edit.Cypher

Lecture



Cypher is the declarative query language for the neo4j, the world's leading graph database.

Key principles and capabilities of Cypher are as follows:

  • Modify the data.

  • Cypher has the concept of identifiers.

  • Cypher can create, update, and remove nodes, labels, and properties.

  • Cypher manages indexes and constraints.

You can try Cypher snippets live at the Console atconsole.neo4j.org or read the Cypher documentation at docs.neo4j.org. For live graph models using Cypher check out GraphGist.

delete all related nodes

CODE:

match (n) - [r] - () DELETE n, r


delete all unrelated nodes (only if they remain)

CODE:

match n DELETE n



create a node with User Label with id: 1 and 2

CODE:

create (n: User {id: 1})
create (n: User {id: 2})


merge edge follow (connection node with property = 1 and = 2)

CODE:

MATCH (a {id: 1}), (b {id: 2}) MERGE (a) - [r: follow] -> (b);




create CONSTRAINT and indexes

CODE:

CREATE CONSTRAINT ON (u: User) ASSERT u. id IS UNIQUE;




find all users (all nodes)

CODE:

MATCH (n: `User`) RETURN n LIMIT 25


(maximum 25 nodes)

Treating every line as a collection (faster):

  LOAD CSV FROM "file: ///tmp/movies.csv" AS csvLine 
  MERGE (p: Person {name: csvLine [0]}) 
  MERGE (m: Movie {title: csvLine [1]}) 
  CREATE (p) - [: PLAYED {role: csvLine [2]}] -> (m) 

 
  or a map: 
  LOAD CSV WITH HEADERS FROM "file: ///tmp/movies.csv" AS csvLine 
  MERGE (p: Person {name: csvLine.name}) 
  MERGE (m: Movie {title: csvLine.title}) 
  CREATE (p) - [: PLAYED {role: csvLine.role}] -> (m) 


  Select all nodes 
MATCH (n) RETURN n; 
  Counter 
 MATCH (n:Ware {wareId:1}) RETURN "Our graph have "+count(*)+" Nodes with label Ware and wareId=1" as counter; 
  Create 2 related nodes 
 CREATE (n{wareId:1})-[r:SUIT]->(m{criteriaId:1}) 

  Graph database NEO4J create, insert, delete, edit.Cypher

  Link 2 existing nodes 
 MATCH (a {wareId: 1}), (b {criteriaId: 2}) MERGE (a)-[r:SUIT]->(b) 
  Choose products that fit criterion 3 
 MATCH (a:Ware)-->(b:Criteria {criteriaId: 3}) RETURN a; 
 

Note: {value} denotes either literals, ad hoc Cypher queries; or parameters, which is the best practice for applications. Neo4j properties can be strings, numbers, booleans or arrays thereof. Cypher also supports maps and collections.

Syntax

RETURN
  RETURN * 

Return the value of all identifiers.

  RETURN n AS columnName 

Use alias for result column name.

  RETURN DISTINCT n 

Return unique rows.

  ORDER BY n.property 

Sort the result.

  ORDER BY n.property DESC 

Sort the result in descending order.

  SKIP {skip_number} 

Skip a number of results.

  LIMIT {limit_number} 

Limit the number of results.

  SKIP {skip_number} LIMIT {limit_number} 

Skip results.

  RETURN count (*) 

The number of matching rows. See Aggregation for more.

CREATE
  CREATE (n {name: {value}}) 

Create a node with the given properties.

  CREATE (n {map}) 

Create a node with the given properties.

  CREATE (n {collectionOfMaps}) 

Create nodes with the given properties.

  CREATE (n) - [r: KNOWS] -> (m) 

Create a relationship with the given type and direction; bind an identifier to it.

  CREATE (n) - [: LOVES {since: {value}}] -> (m) 

Create a relationship with the given type, direction, and properties.

Merge
  MERGE (n: Person {name: {value}})
 ON CREATE SET n.created = timestamp ()
 ON MATCH SET
     n.counter = coalesce (n.counter, 0) + 1,
     n.accessTime = timestamp () 

It doesn’t exist. Use ON CREATE and ON MATCH for conditional updates.

  MATCH (a: Person {name: {value1}}),
       (b: Person {name: {value2}})
 MERGE (a) - [r: LOVES] -> (b) 

MERGE finds or creates a relationship between the nodes.

  MATCH (a: Person {name: {value1}})
 Merge
   (a) - [r: KNOWS] -> (b: Person {name: {value3}}) 

MERGE finds or creates subgraphs attached to the node.

INDEX
  CREATE INDEX ON: Person (name) 

Create an index on the label.

  MATCH (n: Person) WHERE n.name = {value} 

An index can be automatically used for the equality comparison. Note for example lower (n.name) = {value} will not use an index.

  MATCH (n: Person)
 USING INDEX n: Person (name)
 WHERE n.name = {value} 

It can be used when it comes to the index.

  DROP INDEX ON: Person (name) 

Drop the index on the label.

Operators

Mathematical

+, -, *, /,%, ^

Comparison

=, <>, <,>, <=,> =

Boolean

AND, OR, XOR, NOT

String

+

Collection

+, IN, [x], [x .. y]

Regular Expression

= ~

Null
  • NULL is used to represent missing / undefined values.

  • NULL is not equal to NULL. This is not the same as that value. So the expression NULL = NULL yields NULL and not TRUE. To check if an expressoin is NULL, use IS NULL.

  • Arithmetic expressions, comparisons and function calls (except coalesce) will return NULL if any argument is NULL.

  • Collection yields NULL.

  • In OPTIONAL MATCH clauses, NULLs will be used for parts of the pattern.

Labels
  CREATE (n: Person {name: {value}}) 

Create a node with label and property.

  MERGE (n: Person {name: {value}}) 

Matches or creates unique node (s) with label and property.

  SET n: Spouse: Parent: Employee 

Add label (s) to a node.

  MATCH (n: Person) 

Matches nodes labeled as Person.

  MATCH (n: Person)
 WHERE n.name = {value} 

Matches nodes labeled Person with the given name.

  WHERE (n: Person) 

Checks existence of label on node.

  labels (n) 

Labels of the node.

  REMOVE n: Person 

Remove label from node.

Path functions
  length (path) 

The length of the path.

  nodes (path) 

The nodes in the path as a collection.

  relationships (path) 

The relationship in the path as a collection.

  MATCH path = (n) -> (m)
 RETURN extract (x IN nodes (path) | x.prop) 

Assign a path and process its nodes.

  MATCH path = (begin) - [*] -> (end)
 FOREACH
   (n IN rels (path) | SET n.marked = TRUE) 

Execute a mutating operation for each relationship.

Collection functions
  length ({coll}) 

Length of the collection.

  head ({coll}), last ({coll}), tail ({coll}) 

head returns the last element of the collection. tail the remainder of the collection. All return null for an empty collection.

  [x IN coll WHERE x.prop <> {value} |  x.prop] 

Combination of filter and extract in a concise notation.

  extract (x IN coll | x.prop) 

The orignal collection.

  filter (x IN coll WHERE x.prop <> {value}) 

Where the predicate is TRUE.

  reduce (s = "", x IN coll | s + x.prop) 

Accumulate the results.

  FOREACH (value IN coll |
  CREATE (: Person {name: value})) 

Execute a mutating operation for each element in a collection.

Mathematical Functions
  abs ({expr}) 

The absolute value.

  rand () 

A random value. Returns a new value for each call. Also it is useful for selecting subset or random ordering.

  round ({expr}) 

Find the next one up or down.

  sqrt ({expr}) 

The square root.

  sign ({expr}) 

0 if zero, -1 if negative, 1 if positive.

  sin ({expr}) 

Trigonometric functions, also cos, tan, cot, asin, acos, atan, atan2, haversin.

  degrees ({expr}), radians ({expr}), pi () 

Converts radians into degrees, use radians for the reverse. pi for π.

  log10 ({expr}), log ({expr}), exp ({expr}), e () 

Logarithm base 10, natural logarithm, e to the power of the parameter. Value of e.

Aggregation
  count (*) 

The number of matching rows.

  count (identifier) 

The number of non-null values.

  count (DISTINCT identifier) 

All aggregation functions also take the DISTINCTmodifier, which removes duplicates from the values.

  collect (n.property) 

Collection from the values, ignores NULL.

  sum (n.property) 

Sum numerical values. Similar functions are avg, min, max.

  percentileDisc (n.property, {percentile}) 

Discrete percentile. Continuous percentile ispercentileCont. The percentile argument is from 0.0 to1.0.

  stdev (n.property) 

Standard deviation for a sample of a population. For an entire population use stdevp.

CASE
  CASE n.eyes
  WHEN 'blue' THEN 1
  WHEN 'brown' THEN 2
  ELSE 3
 END 

Return THEN value from the matching WHEN value. TheELSE value is optional, and for for NULL if missing.

  CASE
  WHEN n.eyes = 'blue' THEN 1
  WHEN n.age <40 THEN 2
  ELSE 3
 END 

Return THEN value from the first WHEN predicate evaluating to TRUE. Predicates are evaluated in order.

START
  START n = node (*) 

Start from all nodes.

  START n = node ({ids}) 

Start from one or more nodes specified by id.

  START n = node ({id1}), m = node ({id2}) 

Multiple starting points.

  START n = node: nodeIndexName (key = {value}) 

Query the index with an exact query. Use node_auto_indexfor the automatic index.

CREATE UNIQUE
  CREATE UNIQUE
     (n) - [: KNOWS] -> (m {property: {value}}) 

It doesn’t exist. The pattern can not include any optional parts.

Performance
  • Use parameters instead of literals when possible. This allows you to create new execution plans.

  • Always set the variable length patterns. All the nodes in a graph by mistake.

  • Return only the data you need. Avoid returning all nodes and relationships.

Read Query Structure
  [MATCH WHERE]
 [OPTIONAL MATCH WHERE]
 [WITH [ORDER BY] [SKIP] [LIMIT]]
 RETURN [ORDER BY] [SKIP] [LIMIT] 
MATCH
  MATCH (n: Person) - [: KNOWS] -> (m: Person)
 WHERE n.name = "Alice" 

Node patterns can contain labels and properties.

  MATCH (n) -> (m) 

Any pattern can be used in MATCH.

  MATCH (n {name: 'Alice'}) -> (m) 

Patterns with node properties.

  MATCH p = (n) -> (m) 

Assign a path to p.

  OPTIONAL MATCH (n) - [r] -> (m) 

Optional pattern, NULLs will be used for missing parts.

WHERE
  WHERE n.property <> {value} 

Use a predicate to filter. Note that it is always a part of a MATCH, OPTIONAL MATCH, WITH or START clause. Putting it after a different clause.

WITH
  MATCH (user) - [: FRIEND] - (friend)
 WHERE user.name = {name}
 WITH user, count (friend) AS friends
 WHERE friends> 10
 RETURN user 

The WITH syntax is similar to RETURN. It separates the query parts explicitly, allowing you to declare.

  MATCH (user) - [: FRIEND] - (friend)
 WITH user, count (friend) AS friends
 ORDER BY friends DESC
 SKIP 1 LIMIT 3
 RETURN user 

You can also use ORDER BY, SKIP, LIMIT with WITH.

UNION
  MATCH (a) - [: KNOWS] -> (b)
 RETURN b.name
 UNION
 MATCH (a) - [: LOVES] -> (b)
 RETURN b.name 

Returns the union of all query results. Result column types and names have to match.

  MATCH (a) - [: KNOWS] -> (b)
 RETURN b.name
 UNION ALL
 MATCH (a) - [: LOVES] -> (b)
 RETURN b.name 

Returns the union of all query results, including duplicated rows.

Write-Only Query Structure
  (CREATE [UNIQUE] | MERGE) *
 [SET | DELETE | REMOVE | FOREACH] *
 [RETURN [ORDER BY] [SKIP] [LIMIT]] 
Read-Write Query Structure
  [MATCH WHERE]
 [OPTIONAL MATCH WHERE]
 [WITH [ORDER BY] [SKIP] [LIMIT]]
 (CREATE [UNIQUE] | MERGE) *
 [SET | DELETE | REMOVE | FOREACH] *
 [RETURN [ORDER BY] [SKIP] [LIMIT]] 
SET
  SET n.property = {value},
     n.property2 = {value2} 

Update or create a property.

  SET n = {map} 

Set all properties. This will remove any existing properties.

  SET n: Person 

Adds a label Person to a node.

DELETE
  DELETE n, r 

Delete a node and a relationship.

Remove
  REMOVE n: Person 

Remove a label from n.

  REMOVE n.property 

Remove a property.

CONSTRAINT
  CREATE CONSTRAINT ON (p: Person)
        ASSERT p.name IS UNIQUE 

Create a unique constraint on the label. If you want to make a label, it will be updated. This constraint will create an accompanying index.

  DROP CONSTRAINT ON (p: Person)
      ASSERT p.name IS UNIQUE 

Drop in the unique Person and property name.

Patterns
  (n) -> (m) 

A relationship from n to m exists.

  (n: Person) 

Matches nodes with the label Person.

  (n: Person: Swedish) 

Matches nodes that have both Person and Swedishlabels.

  (n: Person {name: {value}}) 

Matches nodes with the declared properties.

  (n: Person) -> (m) 

Node n labeled Person has a relationship to m.

  (n) - (m) 

A relationship in any direction between n and m.

  (m) <- [: KNOWS] - (n) 

KNOWS exists.

  (n) - [: KNOWS | LOVES] -> (m) 

KNOWS or LOVES exists.

  (n) - [r] -> (m) 

Bind an identifier to the relationship.

  (n) - [* 1..5] -> (m) 

Variable length paths.

  (n) - [*] -> (m) 

Any depth. See the performance tips.

  (n) - [: KNOWS] -> (m {property: {value}}) 

Match or set properties in MATCH, CREATE, CREATE UNIQUE orMERGE clauses.

  shortestPath ((n1: Person) - [* .. 6] - (n2: Person)) 

Find a single shortest path.

  allShortestPaths ((n1: Person) -> (n2: Person)) 

Find all shortest paths.

Collections
  ['a', 'b', 'c'] AS coll 

Literal collections are declared in square brackets.

  length ({coll}) AS len, {coll} [0] AS value 

Collections can be passed in as parameters.

  range ({first_num}, {last_num}, {step}) AS coll 

Creates functions of labels, nodes, filters, filter, extract.

  MATCH (a) - [r: KNOWS *] -> ()
 RETURN r AS rels 

Relationship identifiers of a variable length contain a collection of relationships.

  RETURN matchedNode.coll [0] AS value,
        length (matchedNode.coll) AS len 

Properties can be arrays / collections of strings, numbers or booleans.

  coll [{idx}] AS value,
 coll [{start_idx} .. {end_idx}] AS slice 

Collection elements can be accessed with idx subscripts in square brackets. Invalid indexes return NULL. Slices can be omitted or removed. Out of range elements are ignored.

Maps
  {name: 'Alice', age: 38,
  address: {city: 'London', residential: true}} 

Literal maps are in curly braces much like property maps. Nested maps and collections are supported.

  MERGE (p: Person {name: {map} .name})
 ON CREATE SET p = {map} 

Maps or by accessing keys.

  RETURN matchedNode AS map 

Nodes and relationships are the maps of their data.

  map.name, map.age, map.children [0] 

Map entries can be accessed by their keys. Invalid keys result in an error.

Relationship Functions
  type (a_relationship) 

String representation of the relationship type.

  startNode (a_relationship) 

Start node of the relationship.

  endNode (a_relationship) 

End node of the relationship.

  id (a_relationship) 

The internal id of the relationship.

Predicates
  n.property <> {value} 

Use comparison operators.

  has (n.property) 

Use functions.

  n.number> = 1 AND n.number <= 10 

Use boolean operators to combine predicates.

  n: Person 

Check for node labels.

  identifier IS NULL 

Check if something is NULL.

  NOT has (n.property) OR n.property = {value} 

Either property doesn’t exist or predicate is TRUE.

  n.property = {value} 

Non-existing property returns NULL, which is not equal to anything.

  n.property = ~ "Tob. *" 

Regular expression.

  (n) - [: KNOWS] -> (m) 

Make sure the pattern has at least one match.

  NOT (n) - [: KNOWS] -> (m) 

Exclude matches to (n) - [: KNOWS] -> (m) from the result.

  n.property IN [{value1}, {value2}] 

Check if an element exists in a collection.

Collection Predicates
  all (x IN coll WHERE has (x.property)) 

Return to true for all elements of the collection.

  any (x IN coll WHERE has (x.property)) 

Returns true if the predicate is true.

  none (x IN coll WHERE has (x.property)) 

Returns TRUE for all elements of the collection.

  single (x IN coll WHERE has (x.property)) 

Returns TRUE if there is one.

Functions
  coalesce (n.property, {defaultValue}) 

The first non-NULL expression.

  timestamp () 

Milliseconds since midnight, January 1, 1970 UTC.

  id (node_or_relationship) 

The internal id of the relationship or node.

  toInt ({expr}) 

If possible; otherwise it returns NULL.

  toFloat ({expr}) 

If possible; otherwise it returns NULL.

String functions
  str ({expression}) 

String representation of the expression.

  replace ({original}, {search}, {replacement}) 

Replace all occurrences of search with replacement. All arguments are be expressions.

  substring ({original}, {begin}, {sub_length}) 

Get part of a string. The sub_length argument is optional.

  left ({original}, {sub_length}),
   right ({original}, {sub_length}) 

The first part of a string. The last part of the string.

  trim ({original}), ltrim ({original}),
   rtrim ({original}) 

Trim all whitespace, or on left or right side.

  upper ({original}), lower ({original}) 

UPPERCASE and lowercase.

  split ({original}, {delimiter}) 

Split a string into a collection of strings.

Upgrading

With Neo4j 2.0 several Cypher features in version 1.9 have been deprecated or removed.

  • START is optional.

  • CREATE UNIQUE Note that they are not the same, though.

  • OPTIONAL MATCH, not question marks.

  • Non-existing properties return NULL, n.prop? and n.prop! have been removed.

  • For collection functions changed form: to |.

  • Paths are no longer collections, use nodes (path) orrels (path).

  • Parentheses around nodes in patterns are no longer optional.

  • CREATE a = {property: 'value'} has been removed.

  • Use REMOVE to remove properties.

  • Parameters for index-keys and nodes in patterns are no longer allowed.

  • CYPHER statement with CYPHER 1.9.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Databases, knowledge and data warehousing. Big data, DBMS and SQL and noSQL

Terms: Databases, knowledge and data warehousing. Big data, DBMS and SQL and noSQL