Neo4j | Cypher 参考文档

这篇博客详细介绍了Neo4j图数据库的Cypher查询语言,涵盖了模式匹配、停用词、操作、函数以及表达规则和返回语句的使用。通过实例展示了如何使用Cypher进行节点、关系的操作和数据检索,帮助读者掌握图数据库查询技巧。

Neo4j | Cypher 参考文档

!!!这是一篇从 neo4j 开发文档提炼出来, 原文->

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 UNIQUE

  • CSV导入操作: 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 每一个元素满足条件, 返回 true
  • any(variable IN list WHERE predicate) : list 存在一个元素满足条件, 返回 true
  • exists(pattern-or-property) : 存在某种”模式”, 返回 true
  • single(variable IN list WHERE predicate) : list 只有一个元素满足条件, 返回 true
  • none(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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值