一、Basic concepts
Example graph:
1.Nodes:represent entities
2.Labels:group nodes into sets by a certain label
[^one node can get 0 to more lables]:
3.Relationship:
connects two nodes,orgabize nodes into structure,allow a graph to resemble a list,tree,map,compound entity
4.Relationship types
a relationship must have exactly one type.And type can have value with some items
5.Properties(属性)
name-value pairs
[^eg:node Person has two properties:name and born.]:
the value part of th eproperty can hold different data types:number string,bool(refer to “Cypel manual”)
6.Traversals and paths
Traversing a graph means visiting nodes by following relationships according to some rules.
The traversal result could be returned as a path with the length one:
The shorstest path has length zero.(a single node and no relationship)
7.Schema(indexs and constraint)
Neo4j is schema optional.
7.1.index(increase performance)
7.2.constraint:Constraints are used to make sure that the data adheres to the rules of the domain.
Naming rules and recommendations
8.Cyper
9.Pattern(模式:描述所需数据的形状)
A single node or relationship typically encodes very little information, but a pattern of nodes and relationships can encode arbitrarily complex ideas.
Cypher, Neo4j’s query language, is strongly based on patterns. Specifically, patterns are used to match desired graph structures. Once a matching structure has been found or created, Neo4j can use it for further processing.
[^simple pattern:only a single relationship,such as:a Person LIVES_IN a City]:
[^Complex pattern: match instance where a Person LIVES_IN a Country]: (:Person) -[:LIVES_IN]-> (:City) -[:PART_OF]-> (:Country)//图标和箭头用来可视化图表
10.Node syntax
(matrix:Movie {title: “The Matrix”, released: 1997})
()represent a node(anonymous,uncharacterrized node)
(matrix):定义一种变量,只在单个语句中有意义
:Movie is pattern
{title: “The Matrix”, released: 1997} : properties
11.Relationship syntax
-[role:ACTED_IN {roles: ["Neo"]}]->
[^ACTED_IN表示关系的类型(类似于节点的标签]:
(–):无定向关系
在一段加入–>,<–表示定向关系
12.Pattern syntax
(keanu:Person:Actor {name: "Keanu Reeves"} )
-[role:ACTED_IN {roles: ["Neo"] } ]->
(matrix:Movie {title: "The Matrix"} )
[^[]存储关系属性,()存储节点属性]:
13.Pattern variables
To increase modularity and reduce repetition.Cypher允许将模式分配给变量,允许检查匹配路径等其他表达式中。
acted_in = (:Person)-[:ACTED_IN]->(:Movie)
[^nodes(path),relationship(path),length(path):access details of a path]:
二、Patterns in practice
1.Creating data
CREATE (:Movie { title:"The Matrix",released:1997 })
CREATE语句直接创建指定的模式
if 要返回创建的数据,定义一个变量
CREATE (p:Person { name:"Keanu Reeves", born:1964 })
RETURN p
To create more complex structures,we can separate the elements with commas or use multiple CREATE statements
CREATE (a:Person { name:"Tom Hanks", born:1956 })-[r:ACTED_IN { roles: ["Forrest"]}]->(m:Movie { title:"Forrest Gump",released:1994 }) CREATE (d:Person { name:"Robert Zemeckis", born:1951 })-[:DIRECTED]->(m) RETURN a,d,r,m
2.Matching patterns
[^A MATCH statement will search for the patterns we specify and return one row per successful pattern match.we can start looking for all nodes with the Movie label:]:
MATCH (m:Movie)
RETURN m
MATCH (p:Person { name:"Keanu Reeves" })
RETURN p
MATCH (p:Person { name:"Tom Hanks" })-[r:ACTED_IN]->(m:Movie)
RETURN m.title, r.roles
[^可以通过identifer.property访问属性]:
3.Attaching structures
[^To extand the graph with new info]:
MATCH (p:Person { name:"Tom Hanks" })
CREATE (m:Movie { title:"Cloud Atlas",released:2012 })
CREATE (p)-[r:ACTED_IN { roles: ['Zachry']}]->(m)
RETURN p,r,m
4.Completing patterns
Whenever we get data from external systems or are not sure if certain info already exists in the graph,can use MERGE in this function.
MERGE (m:Movie { title:"Cloud Atlas" })
ON CREATE SET m.released = 2012
RETURN m
MATCH (m:Movie { title:"Cloud Atlas" })
MATCH (p:Person { name:"Tom Hanks" })
MERGE (p)-[r:ACTED_IN]->(m)
ON CREATE SET r.roles =['Zachry']
RETURN p,r,m
[^MERGE 还可确保创建不重复的关系,前提是要传递给它两个将要被连接的节点。如果只传递一个节点给MERGE 语句,就会在该节点的直接领居顶点中寻找,不存在则直接创建。]:
CREATE (y:Year { year:2014 })
MERGE (y)<-[:IN_YEAR]-(m10:Month { month:10 })
MERGE (y)<-[:IN_YEAR]-(m11:Month { month:11 })
RETURN y,m10,m11
三、Getting the correct results
1.Filtering results(筛选结果)
用WHERE子句,用AND,OR,XOR and NOT构成了条件语句。
eg:
MATCH (m:Movie) WHERE m.title = "The Matrix" RETURN m
equals:
MATCH (m:Movie { title: "The Matrix" }) RETURN m
WHERE语句还可以用于数值比较,匹配正则公式以及检查列表中是否存在特定值,搭配AND,OR,XOR,NOT
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name =~ "K.+" OR m.released > 2000 OR "Neo" IN r.roles
RETURN p,r,m
MATCH (p:Person)-[:ACTED_IN]->(m)
WHERE NOT (p)-[:DIRECTED]->()
RETURN p,m
2.Returning results
literal value:arrays[1,2,3],map{name:”wrh”,born:2000,movies:[“Forrest”,…]},properties of any node:n.name,length(array),toInteger(“12”), substring(“2014-07-01”,0,4) and coalesce(p.nickname,”n/a”)
起别名expression AS alias
RETURN之后使用DISTINCT返回唯一结果
3.Aggregating information(聚合信息)
count,sum,avg,min,max函数
[^聚合时要跳过NULL值,如果想聚合唯一的DISTINCT值,可以使用count(DISTINCT role)]:
eg:the following statement finds out how often an actor and adirctoe work together
4.Ordering and pagination(排序和分类)
Ordering is using:
ORDER BY expression [ASC|DESC]//ASC正序,DESC逆序
if we return person.name,we can still ORDER BY person.age,because it return the person reference
Pagination is done using SKIP{offset} and LIMIT {count} clauses
For instance to find the most prolific actors we could do:
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) RETURN a, count(*) AS appearances ORDER BY appearances DESC LIMIT 10;
5.Collecting aggregation
use collect() in the parent-child strutures
The following statement could be used to retrieve the cast of each movie in our database:
MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
RETURN m.title AS movie, collect(a.name) AS cast, count(*) AS actors
+-----------------------------------------+
| movie | cast | actors |
+-----------------------------------------+
| "Forrest Gump" | ["Tom Hanks"] | 1 |
| "Cloud Atlas" | ["Tom Hanks"] | 1 |
+-----------------------------------------+
2 rows