目录:[TOC]
RETURN
RETURN 可以返回节点,关系及属性
//返回节点MATCH (n { name: 'B' })RETURN n// 返回关系MATCH (n { name: 'A' })-[r:KNOWS]->(c)RETURN r// 返回属性MATCH (n { name: 'A' })RETURN n.name//返回所有节点关系属性MATCH p =(a)-[r]->(b)RETURN *//不常见字符,使用反引号MATCH (`This isn\'t a common variable`)WHERE `This isn\'t a common variable`.name = 'A'RETURN `This isn\'t a common variable`.happy// 使用别名MATCH (a { name: 'A' })RETURN a.age AS SomethingTotallyDifferent//其他形式的表达式MATCH (a { name: 'A' })RETURN a.age > 30, "I'm a literal",(a)-->()// 去重MATCH (a { name: 'A' })-->(b)RETURN DISTINCT b
WITH
WITH语句允许查询链接在一起,一个查询的结果作为另一个查询的输入WITH变量范围是WITH下面的语句
- 使用WITH和
group by and LIMIT
限制下一部分的输入数量 - 和WHERE结合使用过滤聚合值
- 使用WITH进行读写分析,读的部分只读,写的部分只写
//过滤聚合值,读 WITH 聚合 WHERE//返回和David联系的至少一次外连接的人MATCH (david { name: 'David' })--(otherPerson)-->()WITH otherPerson, count(*) AS foafWHERE foaf > 1RETURN otherPerson.name//在collect之前对数据进行排序和限制// 先读,WITH,排序MATCH (n)WITH nORDER BY n.name DESC LIMIT 3RETURN collect(n.name)//限制下一部分的搜索范围MATCH (n { name: 'Anders' })--(m)WITH mORDER BY m.name DESC LIMIT 1MATCH (m)--(o)RETURN o.name//
UNWIND
UNWIND
解开List成为一行
这个list可能是collect
处理的
UNWIND [1, 2, 3, NULL ] AS xRETURN x, 'val' AS y
//去重WITH [1, 1, 2, 2] AS collUNWIND coll AS xWITH DISTINCT xRETURN collect(x) AS setOfVals// 展开任何返回list的表达式WITH [1, 2] AS a,[3, 4] AS bUNWIND (a + b) AS xRETURN x// 展开list的listWITH [[1, 2],[3, 4], 5] AS nestedUNWIND nested AS xUNWIND x AS yRETURN y//应用空list,返回空UNWIND [] AS emptyRETURN empty, 'literal_that_is_not_returned'// 为了不处理空list,使用CASEWITH [] AS listUNWIND CASE WHEN list = [] THEN [null] ELSE list END AS emptylistRETURN emptylistUNWIND NULL AS xRETURN x, 'some_literal'// 通过参数创建节点{ "events" : [ { "year" : 2014, "id" : 1 }, { "year" : 2014, "id" : 2 } ]}UNWIND $events AS eventMERGE (y:Year { year: event.year })MERGE (y)<-[:IN]-(e:Event { id: event.id })RETURN e.id AS xORDER BY x
WHERE
WHERE是
MATCH, OPTIONAL MATCH and WITH
语句的一部分WHERE变量使用index可以提高查询性能
以下面graph为例:
// 创建节点CREATE (andy:Swedish:Person {name: 'Andy',belt:'white',age:36}) CREATE (peter:Person {name: 'Peter',email:'peter_n@example.com',age:35}) CREATE (timothy:Person {name: 'Timothy',address:'Sweden/Malmo',age:25}) CREATE (fido:Dog {name: 'Fido'}) CREATE (ozzy:Dog {name: 'Ozzy'}) CREATE (andy_dog:Dog {name: 'Andy'}) CREATE (banana:Toy {name: 'Banana'}) // 创建节关系CREATE (andy)-[:HAS_DOG{since:2016}]->(andy_dog)CREATE (andy)-[:KNOWS{since:1999}]->(peter)CREATE (andy)-[:KNOWS{since:2012}]->(timothy)CREATE (peter)-[:HAS_DOG{since:2010}]->(fido)CREATE (peter)-[:HAS_DOG{since:2018}]->(ozzy)CREATE (fido)-[:HAS_TOP]->(banana)
基本用法
//AND, OR, XOR and NOTMATCH (n:Person)WHERE n.name = 'Peter' XOR (n.age < 30 AND n.name = 'Timothy') OR NOT (n.name = 'Timothy' OR n.name = 'Peter')RETURN n.name, n.age//过滤节点标签MATCH (n)WHERE n:SwedishRETURN n.name, n.age//过滤节点属性MATCH (n:Person)WHERE n.age < 30RETURN n.name, n.age//过滤关系属性MATCH (n:Person)-[k:KNOWS]->(f)WHERE k.since < 2000RETURN f.name, f.age, f.email//动态计算属性WITH 'AGE' AS propnameMATCH (n:Person)WHERE n[toLower(propname)]< 30RETURN n.name, n.age//属性是否存在MATCH (n:Person)WHERE EXISTS (n.belt)RETURN n.name, n.belt
字符串匹配
-
STARTS WITH and ENDS WITH and ONTAINS
,大小写敏感
// STARTS WITHMATCH (n:Person)WHERE n.name STARTS WITH 'Pet'RETURN n.name, n.age//ENDS WITH MATCH (n:Person)WHERE n.name ENDS WITH 'ter'RETURN n.name, n.age//CONTAINSMATCH (n:Person)WHERE n.name CONTAINS 'ete'RETURN n.name, n.age//否定MATCH (n:Person)WHERE NOT n.name ENDS WITH 'y'RETURN n.name, n.age
正则匹配
Cypher支持正则表达式,Cypher正则继承与Java的正则
支持改变字符串匹配模式:case-insensitive (?i), multiline (?m) and dotall (?s)
忽略大小写:MATCH (n) WHERE n.name =~ '(?i)Lon.*' RETURN n
使用正则表达式:=~ 'regexp'
MATCH (n:Person)WHERE n.name =~ 'Tim.*'RETURN n.name, n.age//某些字符不使用正则// . or * 使用转义字符MATCH (n:Person)WHERE n.email =~ '.*\\.com'RETURN n.name, n.age, n.email//大小写不敏感MATCH (n:Person)WHERE n.name =~ '(?i)AND.*'RETURN n.name, n.age
路径匹配中使用WHERE
Cypher中模式也是一种表达式
但是模式不仅仅是表达式其是predicatesWHERE不能用新变量,MATCH匹配subgraph,WHERE删除subgraph
//有外连接到timothy的人,WHERE中加入图模式MATCH (timothy:Person { name: 'Timothy' }),(other:Person)WHERE other.name IN ['Andy', 'Peter'] AND (timothy)<--(other)RETURN other.name, other.age//模式中使用NOT不匹配MATCH (person:Person),(peter:Person { name: 'Peter' })WHERE NOT (person)-->(peter)RETURN person.name, person.age//add properties to your patternsMATCH (n:Person)WHERE (n)-[:KNOWS]-({ name: 'Timothy' })RETURN n.name, n.age//过滤关系类型MATCH (n:Person)-[r]->()WHERE n.name='Andy' AND type(r)=~ 'K.*'RETURN type(r), r.since
WHERE中使用子查询
子查询也可以用于指定模式是否存在
子查询使用{}子查询可以使用外部的变量
subquery was introduced in Neo4j 4.0
- 语法格式:
EXISTS { MATCH [Pattern] WHERE [Expression] }
实例:
//一般的子查询MATCH (person:Person)WHERE EXISTS { MATCH (person)-[:HAS_DOG]->(:Dog)}RETURN person.name as name//子查询中加入WHEREMATCH (person:Person)WHERE EXISTS { MATCH (person)-[:HAS_DOG]->(dog :Dog) WHERE person.name = dog.name}RETURN person.name as name//子查询嵌套MATCH (person:Person)WHERE EXISTS { MATCH (person)-[:HAS_DOG]->(dog:Dog) WHERE EXISTS { MATCH (dog)-[:HAS_TOY]->(toy:Toy) WHERE toy.name = 'Banana' }}RETURN person.name as name
WHERE中使用list
//判断是否在list中MATCH (a:Person)WHERE a.name IN ['Peter', 'Timothy']RETURN a.name, a.age//却失的值为null,在判断是为falseMATCH (n:Person)WHERE n.belt = 'white'RETURN n.name, n.age, n.belt//IS NULLMATCH (n:Person)WHERE n.belt = 'white' OR n.belt IS NULL RETURN n.name, n.age, n.beltORDER BY n.name
WHERE中使用范围
MATCH (a:Person)WHERE a.name >= 'Peter'RETURN a.name, a.ageMATCH (a:Person)WHERE a.name > 'Andy' AND a.name < 'Timothy'RETURN a.name, a.age
本文使用 文章同步助手 同步