```
MATCH (:Person { name: 'an' })-[r]->(:Persion)
RETURN type(r) // 返回关系的类型 如:r 就是 'KNOWS'
MATCH (wallstreet { title: 'Wall Street' })<-[:ACTED_IN|:DIRECTED]-(person) RETURN person.name // 使用 “|” 匹配多个关系
```
一般情况下,节点之间的关系描述,夹杂空白字符或者是非文字字符的,但是 ` 使用可以解决这个问题
```merge(n{name:"李雷"})-[r:`IS HIS TEACHER`]->(m{name:"ma"})```
指定关系的深度,或者说是路径的长度
```
MATCH(n{name:"ma"})-[:KNOWS*2]-(m)return m //degree 和path 为2
MATCH(n{name:"ma"})-[:KNOWS*1..2]-(m)return m //degree 和path 为 1 到 2
MATCH p = (n{name:"ma"})-[:KNOWS*2]-(m)return p
MATCH p = (n{name:"ma"})-[:KNOWS*1..2]-(m)return p
// 这两种会把 最长路径上的所有节点和关系都返回
```
设置不同的路径长度,返回的关系值是不同的
```
MATCH p = (n{name:"ma"})-[r:KNOWS]-(m)return r
// {}
MATCH p = (n{name:"ma"})-[r:KNOWS*1]-(m)return r
// [{"identity":1,"start":4,"end":1,"type":"KNOWS","properties":{}}] 4 ,1 为节点的 id
MATCH p = (n{name:"ma"})-[r:KNOWS*2]-(m)return r
// [{"identity":1,"start":4,"end":1,"type":"KNOWS","properties":{}},{"identity":0,"start":1,"end":3,"type":"KNOWS","properties":{"since":2011}}]
MATCH p = (n{name:"ma"})-[r:KNOWS*1..2]-(m)return r
// [{"identity":1,"start":4,"end":1,"type":"KNOWS","properties":{}}],
//[{"identity":1,"start":4,"end":1,"type":"KNOWS","properties":{}},{"identity":0,"start":1,"end":3,"type":"KNOWS","properties":{"since":2011}}]
MATCH p = (n{name:"an"})--(m)return id(m) //返回 节点的 id
MATCH p = (n{name:"an"})--(m) where id(m) = 3 return m
```
```
Zero length paths
Using variable length paths that have the lower bound zero means that two variables can point to the same node. If the path length between two nodes is zero, they are by definition the same node. Note that when matching zero length paths the result may contain a match even when matching on a relationship type not in use.
```
使用变量表示路径,如果关系中有0 ,那么这两个变量指向的是同一个节点。如果两个节点之间的关系路径深度为0 ,那么这个两个节点其实是同一个节点。注意:如果匹配 0 长度时,即使没有指定关系类型,也只会返回一个结果(也就是节点自身)
如果返回或者过滤模式图中的路径,可以使用命名路径
```
MATCH p =(michael { name: 'Michael Douglas' })-->() RETURN p
```
如果关系不指定方向,那么就会返回节点两个方向(进/出)的关系。
```
match(n{name:"sun"}),(e{name:"ma"}),p = shortestPath((n)-[*..3]->(e))return p
// 利用shortestPath()函数 返回一个两个节点的最短路径。两个节点之间的关系,可以有方向,也没有不指定方向
```
使用where条件语句对结果进行过滤或者限定
```
match(n{name:"sun"}),(e{name:"ma"}),p = shortestPath((n)-[*..3]-(e))where NONE (r in relationships(p) where type(r) = 'KNOWS') return p
// none 貌似起到了not的作用。where限定了两个节点之间不能是 KNOWS 关系。
// type(r) 的返回值是 关系的类型。 relationships()函数返回路径中的关系
match(n{name:"sun"}),(e{name:"ma"}),p = shortestPath((n)-[*..3]-(e)) return relationships(p)
//[{"identity":0,"start":1,"end":3,"type":"KNOWS","properties":{"since":2011}},{"identity":1,"start":4,"end":1,"type":"KNOWS","properties":{}}]
match(n{name:"sun"}),(e{name:"ma"}),p = allShortestPaths((n)-[*..3]-(e)) return p
// allShortestPaths() 返回所有的路径?!
MATCH (n) WHERE id(n) IN [0, 3, 5] RETURN n
//id()可以返回节点,关系的id
optional match 当匹配的关系/节点,不存在时,返回Null
match(n{name:"ma"}) optional match(n)<-[r]-(e) return e
// null
distinct 如果匹配到多个结果,但是只返回一个
match(n)-[r]-(m)return r
// 会返回多个结果,这些结果里面可能有重复的
match(n)-[r]-(m)return distinct r
//返回多个结果,但是重复的只返回一次。
unwind //解包
match(n)where exists(n.klout)return n.name
// exists限定了,返回具有klout 属性的节点。
match(n) where n.name starts with 'E' return n.name
// ends with 以xx结尾
//contains 包含
match(n)where not n.name ends with 'e' return n.name
// not 放在where 之后,紧挨着 where
match(n)where n.name =~'E.+'return n
// 正则表达式
```