:play cypher
可以打开Cypher的帮助文档
Cypher
Neo4j’s graph query language
Neo4j’s Cypher language is purpose built for working with graph data.
- uses patterns to describe graph data 使用模式描述图形数据
- familiar SQL-like clauses 熟悉的类似SQL的查询语句
- declarative, describing what to find, not how to find it 说明性的,描述要查找的内容,而不是如何查找
CREATE
Create a node
Let’s use Cypher to generate a small social graph.
CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
CREATE
clause to create data CREATE语句去添加数据()
parenthesis to indicate a node 括号表示一个节点ee:Person
a variable ‘ee’ and label ‘Person’ for the new node- brackets to add properties to the node 大括号里面的内容为节点添加属性
MATCH
Finding nodes
Now find the node representing Emil:
MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;
MATCH
clause to specify a pattern of nodes and relationships(ee:Person)
a single node pattern with label ‘Person’ which will assign matches to the variable ‘ee’WHERE
clause to constrain the resultsee.name = "Emil"
compares name property to the value “Emil”RETURN
clause used to request particular results
CREATE more
Nodes and relationships
CREATE
clauses can create many nodes and relationships at once.
MATCH (ee:Person) WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally)
Pattern matching
Describe what to find in the graph
For instance, a pattern can be used to find Emil’s friends:
MATCH (ee:Person)-[:KNOWS]-(friends)
WHERE ee.name = "Emil" RETURN ee, friends
MATCH
clause to describe the pattern from known Nodes to found Nodes(ee)
starts the pattern with a Person (qualified by WHERE)-[:KNOWS]-
matches “KNOWS” relationships (in either direction)(friends)
will be bound to Emil’s friends
Recommend
Using patterns
Pattern matching can be used to make recommendations. Johan is learning to surf, so he may want to find a new friend who already does:
MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer
()
empty parenthesis to ignore these nodesDISTINCT
because more than one path will match the patternsurfer
will contain Allison, a friend of a friend who surfs
Analyze
Using the visual query plan
Understand how your query works by prepending EXPLAIN
or PROFILE
:
PROFILE MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer
Live Cypher warnings
Identify query problems in real time
As you type, the query editor notifies you about deprecated features and potentially expensive queries.
Next steps
Start your application using Cypher to create and query graph data. Use the REST API to monitor the database. In special cases, consider a plugin.
Keep getting started
Intro
- a guided tour
Concepts
- GraphDB 101
The Movie Graph
- create the movie graph
Northwind Graph
- from RDBMS to graph
Reference
Full Northwind import example
Cypher Refcard
The Cypher chapter of the Neo4j Developer Manual