One: node syntax
In cypher, a node is represented by a pair of parentheses (), which is queried in the Cypher form as follows:
1, () means match any one of the nodes
2, (Node1) represents the match of any node and gives it an alias
3, (: lable) represents querying a type of data
4, (person:lable) represents querying a type of data, and gives it an alias
5, (person:lable {name: "Xiao Wang"}) query data under a type where the node property satisfies a value
6, (person:lable {name: "Xiao Wang", age:23}) the attributes of a node can exist in more than one, and is a relationship of an and
Two: Relational grammar
Relationship with a pair-composition, the relationship between the direction of the in and out, if there is no direction is to enter and out both query
1,--> point to a Node
2,-[role]-> the relationship with an individual name
3,-[:acted_in]-> access to a class of relationships
4,-[role:acted_in]-> accesses a class of relationships and adds aliases
5,-[role:acted_in {roles:["Neo", "Hadoop"]}]->
Data that accesses a relationship of a property under a class of relationship
Three: Pattern syntax
Schema syntax is a combination of node and relational query syntax, and we can do whatever complex query we want with the schema syntax.
(P1:Person:Actor {name: "Tom"})-[role:acted_in {roles:["Neo", "Actor"]}]-(M1:movie {title: "Water"})
Four: Mode variable
To increase modularity and reduce duplication, Cypher allows the result of a pattern to be specified in a variable or alias for subsequent use or operation
Path = (: Person)-[:acted_in]-> (: Movie)
Path is the abstract encapsulation of the result set, and there are several functions that can extract data directly from path, such as:
Nodes (PATH): Extract all Nodes
Rels (PATH): Extract all relationships and relationships (path) equal
Length (path): Gets the path length
V: Conditions
Cypher statements are also made up of multiple keywords, like SQL's
Select Name, COUNT (*) from Talbe where age=24 group by name have Count (*) >2 ORDER by COUNT (*) desc
The syntax of multiple keywords, cypher is very similar, each keyword executes a specific task to process the data
Match: Key keywords for queries
Create: Insert similar to SQL
Filter,project,sort,page and so on have the corresponding function statement
By combining some of the above statements, we can write very powerful and complex syntax to query what we want to retrieve, and Cypher automatically parses the syntax and optimizes execution.
Some practical usage examples: 1: Create
Create (: Movie {title: "ABC", released:2016}) return p;
2: Query
Match (P:person) return p; Querying all data for the person type
Match (P:person {name: "Sun"}) return p; Search for people whose names are equal to sun
Match (P1:person {name: "Sun"})-[rel:friend]-> (p2) return p2.name, p2.age Query Sun's friend's name and age
Match (old) ... create (new)-[rel:dr]-> (new) return new to the existing node and the newly created node 3: Query or update
The merge syntax can make no changes to the existing nodes, merging the changed parts
MERGE (M:movie {title: "Cloud Atlas"}) on
CREATE SET m.released =
RETURN m
Merge .... On create set ... return syntax supports merge updates
4: Filter Filter
Cypher filtering is also used with the same keywords as SQL where
Match (P1:person) where p1.name= "Sun" return p1;
Equivalent to the following
Match (P1:person {name: "Sun"}) return P1
Note that in the Where condition the Boolean operators such as and, or, xor,not are supported, and inside the JSON string are
In addition, the Where query also supports regular queries
Match (P1:person)-[r:friend]-> (P2:person)
where p1.name=~ "k.+" or p2.age=24 or "Neo" in R.rels
return p1,r, P2
Relationship filtering matching using not
MATCH (P:person)-[:acted_in]-> (m)
WHERE not (p)-[:D irected]-> ()
RETURN p,m
5: Result set returns
MATCH (P:person)
RETURN p, p.name as name, Upper (P.name), COALESCE (P.nickname, "N/A") as nickname, {name:p.name,
Label:head (Labels (p))} as Person
The result set returns to do the redo
Match (n) return distinct n.name;
6: Aggregate function
Cypher Support Count,sum,avg,min,max
Match (: Person) return count (*)
When aggregating, NULL is skipped count syntax support count (distinct role)
MATCH (Actor:person)-[:acted_in]-> (movie:movie) <-[:D irected]-(director:person)
RETURN Actor,director, COUNT (*) as collaborations
7: Sorting and Paging
MATCH (A:person)-[:acted_in]-> (m:movie)
RETURN a,count (*) as appearances
ORDER by appearances DESC SKIP 3 LIMIT 10;
8: Collect aggregated results
MATCH (M:movie) <-[:acted_in]-(A:person)
RETURN m.title as Movie, collect (a.name) as cast, COUNT (*) as actors
9:union Union
Supports two query structure set-like results merging
MATCH (Actor:person)-[r:acted_in]-> (movie:movie)
RETURN actor.name as name, type (r) as acted_in, Movie.title as Title
UNION (All)
MATCH (Director:person)-[r:directed]-> (movie:movie)
RETURN director.name as name, Type (r) as acted_in, Movie.title as Title
10:with
The WITH statement provides Cypher with a powerful pipeline capability that can be one or query output, or the input of the next query is very similar to the return statement, except that with each result, the alias identifier must be used.
Through this function, we can easily do in the query results in the continuation of nested queries.
MATCH (Person:person)-[:acted_in]-> (M:movie) with person
, COUNT (*) as appearances, Collect (M.title) as movies
WHERE Appearances > 1
RETURN Person.name, appearances, movies
11: Add a constraint or index
Unique constraint (implemented using merge) CREATE CONSTRAINT on (movie:movie) ASSERT Movie.title is unique
Add indexes (quickly find the start node when the graph is traversed), greatly improving query traversal performance CREATE INDEX On:actor (name)
To add test data:
CREATE (Actor:actor {name: "Tom Hanks"}), (Movie:movie {title: ' Sleepless in Seattle '}), (
actor)-[:acted_in]-> ( Movie);
Using index Queries:
MATCH (Actor:actor {name: "Tom Hanks"})
RETURN actor;