- 深解Cypher中的聚合
- 值或计数的聚合要么从查询返回,要么用作多步查询下一部分的输入。
- 查看数据模型
- CALL db.schema.visualization()
- CALL db.schema.visualization()
- 查看图中节点的属性类型
- CALL db.schema.notetypeproperties()
- CALL db.schema.notetypeproperties()
- 查看图中关系的属性类型
- CALL db.schema.reltypeproperties()
- CALL db.schema.reltypeproperties()
- Cypher中的聚合
- 列表
- 列表是包含元素的数组。列表中的元素不必都是同一类型。
- 使用 [ ]
- MATCH (m:Movie) RETURN [m.title, m.released, date().year - date(m.released).year + 1 ]
- 使用 collect()
- MATCH (a:Actor)--(m:Movie) WITH a,collect(m.title) AS Movies RETURN a.name AS Actor,Movies LIMIT 10
- 工作原理
- 返回一个元素列表。可以 collect() 在查询期间随时使用创建列表。在查询期间创建列表时,会发生聚合。
- 在聚合期间,图形引擎通常根据行中的某个值对数据进行分组。(非聚合值是分组键)
- Examples
- 收集节点
- MATCH (m:Movie) UNWIND m.languages AS language WITH language, collect(m) AS movies MERGE (l:Language {name:language}) WITH l, movies UNWIND movies AS m WITH l,mMERGE (l)<-[:SPEAKS]-(m)
- 以作为 language 分组键,收集节点
- 使用 nodes() 返回路径中的节点列表。
- MATCH path = (p:Person {name: 'Elvis Presley'})-[*4]-(a:Actor) WITH nodes(path) AS n UNWIND n AS x WITH x WHERE x:Movie RETURN DISTINCT x.title
- 收集关系
- MATCH (u:User {name: "Misty Williams"})-[x]->() WITH collect(x) AS ratings UNWIND ratings AS r WITH r WHERE r.rating <= 1 RETURN r.rating AS Rating, endNode(r).title AS Title
- 使用 endNode() 返回关系末尾的节点。
- 与子查询
- PROFILE MATCH (m:Movie)<-[:ACTED_IN]-(p:Person) WITH m, collect(p.name) AS Actors WHERE size(Actors) <= 3 RETURN m.title AS Movie, Actors
- 查询重写
- PROFILE CALL { MATCH (m:Movie)<-[:ACTED_IN]-(p:Person) WITH m , collect(p.name) as Actors WHERE size(Actors)<= 3 RETURN m.title as Movie, Actors } RETURN Movie, Actors
- 使用 count()
- 可以在查询处理期间对属性、节点、关系、路径或行进行计数。
- 在属性值上使用
- MATCH (p:Person) RETURN count(p) , count(p.born),count(p.name)
- 此查询看到 born 的计数与其它不同,说明节点中无 born 属性
- MATCH (p:Person) RETURN count(p) , count(p.born),count(p.name)
- 作为过滤查询的子句
- MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WITH a, count(*) AS NumMovies, collect(m.title) AS Movies WHERE NumMovies = 2 RETURN a.name AS Actor,Movies
- 计算节点数
- MATCH (p:Person {name: 'Elvis Presley'})-[]-(m:Movie)-[]-(a:Actor) RETURN count(*), count(m), count (a)
- 使用模式理解
- 模式理解的行为类似于使用 OPTIONAL MATCH,并且对于这个特定的查询,经过的时间减少了。
- 原
- 模式
- PROFILE MATCH (m:Movie) WHERE m.year = 2015 RETURN m.title AS Movie,[(dir:Person)-[:DIRECTED]->(m) | dir.name] AS Directors,[(actor:Person)-[:ACTED_IN]->(m) | actor.name] AS Actors
- 模式理解条件的过滤
- MATCH (a:Person {name: 'Tom Hanks'}) RETURN [(a)--(b:Movie) WHERE b.title CONTAINS "Toy" | b.title ]AS Movies
- 返回的列表添加更多属性(相当于Oracle中的合并列)
- 相当于不重复属性的collect(),注:模式理解下的属性可能为0,所有用size()定义
- MATCH (a:Actor)-[:ACTED_IN]->(m:Movie) WHERE 2000 <= m.year <= 2005 AND a.born.year >= 1980 RETURN a.name AS Actor, a.born AS Born,collect(DISTINCT m.title) AS Movies ORDER BY Actor
- MATCH (a:Actor) WHERE a.born.year >= 1980 WITH a, [(a)-[:ACTED_IN]->(m:Movie) WHERE 2000 <= m.year <= 2005 | m.title] AS Movies WHERE size(Movies) > 0 RETURN a.name as Actor, a.born as Born, Movies
- MATCH (a:Person {name: 'Tom Hanks'}) RETURN [(a)--(b:Movie) WHERE b.title CONTAINS "Toy" | b.title + ": " + b.year] AS Movies
- 相当于不重复属性的collect(),注:模式理解下的属性可能为0,所有用size()定义
- 模式理解的行为类似于使用 OPTIONAL MATCH,并且对于这个特定的查询,经过的时间减少了。
- 列表
Cypher中的聚合
最新推荐文章于 2023-07-28 09:33:44 发布