Neo4j | Cypher 参考文档
Patterns and pattern-matching 是 Cypher 的精髓
1. Patterns 模式
node|label|type|property|path
- (a) 节点
- (a)–(b) 相关节点模式
- (a:LABEL1:LABEL2) 指定label模式
- (a {key:”value”}) 指定属性的模式
- (a)-[r:TYPE1|TYPE2]-(b) 指定关系类型模式
- (a)-[r*2..5]->(b) 可变长度的模式
- p = (a)-[*2..5]->(b) 指定路径的模式
2. Clauses 停用词
基本上弄清楚每个停用词的用法, Cypher 算是入门了, 这里就不展开了
- CREATE
- MATCH
- MERGE
- WHERE
- SET, REMOVE, DELETE, DETACH DELETE, FOREACH
- WITH
- UNWIND
- RETURN
- ORDER BY
- SKIP
- LIMIT
示例:
match (neo:label) where neo.name="NEO4J" return neo order by neo.age skip 2 limit 10
3. Operators 操作
属性操作: . for property access, [] for dynamic property access数学操作:+, -, *, /, %, ^逻辑操作: =, <>, <, >, <=, >=, IS NULL, IS NOT NULL, AND, OR, XOR, NOT图操作符: STARTS WITH, ENDS WITH, CONTAINS正则操作: =~ for regex matching集合操作: IN to check existence of an element in a list, [] for accessing element(s)schedule操作: DISTINCT列表遍历操作: FOREACH(item in nodes(p) | SET n.marked = TRUE)索引操作: CREATE INDEX ON :Person(firstname, surname)约束操作: CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUECSV导入操作: LOAD CSV FROM ‘https://neo4j.com/docs/developer-manual/3.3/csv/artists.csv’ AS line
CREATE (:Artist { name: line[1], year: toInteger(line[2])})
4. Function 函数
知道什么场景下使用什么函数, 就算进阶了
4.1 Predicate functions
all(variable IN list WHERE predicate): list 每一个元素满足条件, 返回 trueany(variable IN list WHERE predicate): list 存在一个元素满足条件, 返回 trueexists(pattern-or-property): 存在某种”模式”, 返回 truesingle(variable IN list WHERE predicate): list 只有一个元素满足条件, 返回 truenone(variable IN list WHERE predicate): list 没有一个元素满足条件, 返回 true
示例:
MATCH p =(a)-[*1..3]->(b)
WHERE a.name = 'Alice' AND b.name = 'Daniel' AND ALL (x IN nodes(p) WHERE x.age > 30)
RETURN p
All nodes in the returned paths will have an age property of at least '30'.
----------------------------------------------------
MATCH (n)
WHERE exists(n.name)
RETURN n.name AS name, exists((n)-[:MARRIED]->()) AS is_married
4.2 Scalar functions
- endNode()
- startNode()
- id()
- length()
- size()
- type()
- properties()
4.3 Aggregating functions
- avg()
- collect()
- count()
- max()
- min()
- sum()
4.4 List functions
extract(): map 操作reduce(): reduce 操作filter(): 过滤操作keys(): 获取节点的属性集labels(): 获取节点的标签集nodes(): 获取路径中的节点集relationship(): 获取路径的关系集range(): 自定义列表
示例:
MATCH p =(a)-->(b)
WHERE a.name = 'Alice' AND b.name = 'Bob'
RETURN extract(n IN nodes(p)| n.age) AS extracted
-----------------------------------------
MATCH (a)
WHERE a.name = 'Eskil'
RETURN a.array, filter(x IN a.array WHERE size(x)= 3)
-------------------------------
MATCH p =(a)-->(b)-->(c)
WHERE a.name = 'Alice' AND b.name = 'Bob' AND c.name = 'Daniel'
RETURN reduce(totalAge = 0, n IN nodes(p)| totalAge + n.age) AS reduction
5. Expression 表达规则
An expression in Cypher can be:
- A decimal (integer or double) literal: 13, -40000, 3.14, 6.022E23.
- A hexadecimal integer literal (starting with 0x): 0x13zf, 0xFC3A9, -0x66eff.
- An octal integer literal (starting with 0): 01372, 02127, -05671.
- A string literal: 'Hello', "World".
- A boolean literal: true, false, TRUE, FALSE.
- A variable: n, x, rel, myFancyVariable, `A name with weird stuff in it[]!`.
- A property: n.prop, x.prop, rel.thisProperty, myFancyVariable.`(weird property name)`.
- A dynamic property: n["prop"], rel[n.city + n.zip], map[coll[0]].
- `A parameter: $param, $0`
- A list of expressions: ['a', 'b'], [1, 2, 3], ['a', 2, n.property, $param], [ ].
- A function call: length(p), nodes(p).
- An aggregate function: avg(x.prop), count(*).
- `A path-pattern: (a)-->()<--(b).`
- An operator application: 1 + 2 and 3 < 4.
- A predicate expression is an expression that returns true or false: a.prop = 'Hello', length(p) > 10, exists(a.name).
- A regular expression: a.name =~ 'Tob.*'
- A case-sensitive string matching expression: a.surname STARTS WITH 'Sven', a.surname ENDS WITH 'son' or a.surname CONTAINS 'son'
- A CASE expression.
6. Return 返回
return 的类型有三种 list, map, record
这篇博客详细介绍了Neo4j图数据库的Cypher查询语言,涵盖了模式匹配、停用词、操作、函数以及表达规则和返回语句的使用。通过实例展示了如何使用Cypher进行节点、关系的操作和数据检索,帮助读者掌握图数据库查询技巧。
562

被折叠的 条评论
为什么被折叠?



