Neo4j Notes - Cypher Manual Chapter 3

3 Clauses

See simpler version at this doc
See full introduction here

3.1 MATCH

[图片上传失败...(image-e8a3c3-1553048005237)]

3.1.1 Intro

3.1.2 Basic node finding

Too simple, omitted.

3.1.3 Relationship basics

Too simple, omitted.

3.1.4 Relationships in depth

3.1.4.1 Relationship types with uncommon characters

MATCH (charlie:Person { name: 'Charlie Sheen' }),(rob:Person { name: 'Rob Reiner' })
CREATE (rob)-[:`TYPE
WITH SPACE`]->(charlie)

Which leads to the following graph:


Graph 2.png

3.1.4.2 Multiple relationships

MATCH (charlie { name: 'Charlie Sheen' })-[:ACTED_IN]->(movie)<-[:DIRECTED]-(director)
RETURN movie.title, director.name

3.1.4.3. Variable length relationships

using the following syntax: -[:TYPE*minHops..maxHops]→. minHops and maxHops are optional and default to 1 and infinity respectively.

MATCH (martin { name: 'Charlie Sheen' })-[:ACTED_IN*1..3]-(movie:Movie)
RETURN movie.title

result:

movie.title
"Wall Street"
"The American President"
"The American President"

3.1.4.4 Relationship variable in variable length relationships

When the connection between two nodes is of variable length, the list of relationships comprising the connection can be returned using the following syntax:

MATCH p =(actor { name: 'Charlie Sheen' })-[:ACTED_IN*2]-(co_actor)
RETURN relationships(p)
relationships(p)
[:ACTED_IN[0]{role:"Bud Fox"},:ACTED_IN[1]{role:"Carl Fox"}]
[:ACTED_IN[0]{role:"Bud Fox"},:ACTED_IN[2]{role:"Gordon Gekko"}]

3.1.4.5. Match with properties on a variable length path

MATCH (charlie:Person { name: 'Charlie Sheen' }),(martin:Person { name: 'Martin Sheen' })
CREATE (charlie)-[:X { blocked: FALSE }]->(:UNBLOCKED)<-[:X { blocked: FALSE }]-(martin)
CREATE (charlie)-[:X { blocked: TRUE }]->(:BLOCKED)<-[:X { blocked: FALSE }]-(martin)
image.png
MATCH p =(charlie:Person)-[* { blocked:false }]-(martin:Person)
WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen'
RETURN p

result:

p
(0)-[X,20]->(20)<-[X,21]-(1)

3.1.4.6 Zero length paths

If the path length between two nodes is zero, they are by definition the same node.

MATCH (wallstreet:Movie { title: 'Wall Street' })-[*0..1]-(x)
RETURN x

Returns the movie itself as well as actors and directors one relationship away

3.1.4.7 Named paths

MATCH p =(michael { name: 'Michael Douglas' })-->()
RETURN p

result:

p
(2)-[ACTED_IN,5]->(6)
(2)-[ACTED_IN,2]->(5)

3.1.4.8. Matching on a bound relationship

MATCH (a)-[r]-(b)
WHERE id(r)= 0
RETURN a,b
a                                                  b
Node[0]{name:"Charlie Sheen"}                      Node[5]{title:"Wall Street"}

Node[5]{title:"Wall Street"}                       Node[0]{name:"Charlie Sheen"}

3.1.5 Shortest path

3.1.5.1. Single shortest path

MATCH (martin:Person { name: 'Martin Sheen' }),(oliver:Person { name: 'Oliver Stone' }), p = shortestPath((martin)-[*..15]-(oliver))
RETURN p

3.1.5.2. Single shortest path with predicates

MATCH (charlie:Person { name: 'Charlie Sheen' }),(martin:Person { name: 'Martin Sheen' }), p = shortestPath((charlie)-[*]-(martin))
WHERE NONE (r IN relationships(p) WHERE type(r)= 'FATHER')
RETURN p

3.1.5.3. All shortest paths

MATCH (martin:Person { name: 'Martin Sheen' }),(michael:Person { name: 'Michael Douglas' }), p = allShortestPaths((martin)-[*]-(michael))
RETURN p

3.1.6. Get node or relationship by id

3.1.6.1 Node by id

3.1.6.2 Relationship by id

3.1.6.3 Multiple nodes by id

MATCH (n)
WHERE id(n) IN [0, 3, 5]
RETURN n

3.2 OPTIONAL MATCH

3.2.1 Introduction

OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.

3.2.2 Optional Relationships

MATCH (a:Movie { title: 'Wall Street' })
OPTIONAL MATCH (a)-->(x)
RETURN x

return <null>

3.2.3 Properties on optional elements

MATCH (a:Movie { title: 'Wall Street' })
OPTIONAL MATCH (a)-->(x)
RETURN x, x.name

return <null> <null>

3.2.4. Optional typed and named relationship

MATCH (a:Movie { title: 'Wall Street' })
OPTIONAL MATCH (a)-[r:ACTS_IN]->()
RETURN a.title, r

return "Wall Street" <null>

3.4 RETURN

Too simple, omitted part content.

3.4.5. Return all elements

MATCH p =(a { name: 'A' })-[r]->(b)
RETURN *

will return two nodes, the relationship and the path used in the query.

3.4.10. Unique results

MATCH (a { name: 'A' })-->(b)
RETURN DISTINCT b

3.5 WITH

3.5.1 Introduction

Using WITH, you can manipulate the output before it is passed on to the following query parts.
Three common usage:

  • to limit the number of entries that are then passed on to other MATCH clauses.
  • to filter on aggregated values. WITH is used to introduce aggregates which can then be used in predicates in WHERE.
  • to separate reading from updating of the graph.

3.5.2 Filter on aggregate function results

MATCH (david { name: 'David' })--(otherPerson)-->()
WITH otherPerson, count(*) AS foaf
WHERE foaf > 1
RETURN otherPerson.name

return "Anders"

3.5.3 Sort results before using collect on them

MATCH (n)
WITH n
ORDER BY n.name DESC LIMIT 3
RETURN collect(n.name)

return ["George","David","Ceasar"]

3.5.4. Limit branching of a path search

MATCH (n { name: 'Anders' })--(m)
WITH m
ORDER BY m.name DESC LIMIT 1
MATCH (m)--(o)
RETURN o.name

return

"Bossman"
"Anders"

3.6 UNWIND

Read Later

3.7 WHERE

3.7.1 Introduction

In the case of WITH and START, WHERE simply filters the results.
For MATCH and OPTIONAL MATCH on the other hand, WHERE adds constraints to the patterns described. It should not be seen as a filter after the matching is finished.

image.png

3.7.2 Basic usage

Too simple, most part omitted.

3.7.2.2 Filter on node label

To filter nodes by label, write a label predicate after the WHERE keyword using WHERE n:foo.

MATCH (n)
WHERE n:Swedish
RETURN n.name, n.age

3.7.2.6. Property existence checking

Use the exists() function to only include nodes or relationships in which a property exists.

MATCH (n)
WHERE exists(n.belt)
RETURN n.name, n.belt

3.7.3 String matching

3.7.3.1. Prefix string search using STARTS WITH

MATCH (n)
WHERE n.name STARTS WITH 'Pet'
RETURN n.name, n.age

3.7.3.2. Suffix string search using ENDS WITH

MATCH (n)
WHERE n.name ENDS WITH 'ter'
RETURN n.name, n.age

3.7.3.3. Substring search using CONTAINS

MATCH (n)
WHERE n.name CONTAINS 'ete'
RETURN n.name, n.age

3.7.3.4. String matching negation

use the NOT keyword to exclude all matches on given string

MATCH (n)
WHERE NOT n.name ENDS WITH 'y'
RETURN n.name, n.age

3.7.4 Regular expressoiins

=~

3.7.5 Using path patterns in WHERE

3.7.5.1 Filter on patterns

MATCH (timothy { name: 'Timothy' }),(others)
WHERE others.name IN ['Andy', 'Peter'] AND (timothy)<--(others)
RETURN others.name, others.age

return "Andy" 36

3.7.5.2 using NOT

3.7.5.3 Filter on patterns with properties

MATCH (n)
WHERE (n)-[:KNOWS]-({ name: 'Timothy' })
RETURN n.name, n.age

return "Andy" 36

3.7.5.4. Filter on relationship type

MATCH (n)-[r]->()
WHERE n.name='Andy' AND type(r)=~ 'K.*'
RETURN type(r), r.since

3.7.6 Lists

MATCH (a)
WHERE a.name IN ['Peter', 'Timothy']
RETURN a.name, a.age
"Timothy"  25
"Peter"  35

3.7.7 Missing properties and values

Read Later

3.7.8 Using ranges

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • 一、常用命令 二、实战演练
    AsaGuo阅读 148评论 0 0
  • NO.33 中计 殷怜安被一阵折腾,又强迫着出了大牢被压进了一辆黑车,过了没一会儿就看到了一处别墅。 “到了,走吧...
    初琰阅读 258评论 0 1
  • 前文我提到过,我们写文章的目的是要有转化,不是为了表面繁荣的阅读数字而自嗨,那是没有任何意义的。 一个潜在客户的转...
    中域李腾飞阅读 8,036评论 138 145
  • 有句俗话说“光脚的不怕穿鞋的”,可是如今世事颠倒,成了穿鞋的不怕光脚的。 今年CBA刚一开打,就吸引了众多目光,倒...
    听风阁主人阅读 1,182评论 1 3