首页 > 解决方案 > Neo4j Cypher - 如何限制 50% 的 MATCH 结果

问题描述

我想限制 50% 的结果,MATCH但它看起来LIMIT不接受动态值。

我试过:

MATCH (:Profile) 
WITH COUNT(*) AS c
MATCH (n:Profile)
WITH n ORDER BY rand() LIMIT toInt(c * 0.5) 
RETURN n

然后我得到了错误:

It is not allowed to refer to variables in LIMIT

那么有没有办法在不使用 2 个单独的查询的情况下做到这一点?

标签: neo4jcypherlimit

解决方案


这就是我的看法。

  1. 获取所有配置文件并为每一行创建一个随机数
  2. 将所有配置文件收集到配置文件列表中并按随机数 (r) 排序
  3. 计算配置文件列表大小的 50%
  4. 展开列表从 start 到 cnt 然后返回每个节点
MATCH (n:Profile) 
WITH n, rand() as r ORDER by r 
WITH collect(n) as profile_lst
WITH profile_lst, toInt(size(profile_lst)/2) as cnt
UNWIND profile_lst[0..cnt] as prof
RETURN prof

推荐阅读