我们有一个项目,我们使用Spring Data Neo4J . 其中一个重要实体如下所示:
@NodeEntity
public class Category {
@GraphId
Long id;
String name;
@RelatedTo(direction = Direction.INCOMING, type = "CHILD")
Category parent;
@RelatedTo(direction = Direction.OUTGOING, type = "CHILD")
Set<Category> children;
}
我们需要从名称已知的特定类别开始查找所有叶子类别(即没有任何子节点的类别) . 例如,给定如下所示的层次结构:
Electronics
Camera
Point and Shoot
SLR
Computing
Desktop
Laptop
Tablet
Netbook
Furniture
Tables
Office tables
Home tables
Chairs
Lounge chairs
Office chairs
搜索“家具”应返回“办公桌”,“家庭桌”,“休闲椅”和“办公椅” . 同样,搜索“计算”应返回“桌面”,“笔记本电脑”,“平板电脑”和“上网本” .
需要帮助来创建一个cypher查询,该查询可以放在Spring Data存储库方法上,从而为我提供从指定节点开始的所有叶节点 .
EDIT 以下查询(使用关联的Spring Data存储库方法)在Wes的帮助下工作:
@Query(
"START category=node:__types__(className='org.example.domain.Category') " +
"MATCH category-[:CHILD*0..]->child " +
"WHERE category.name={0} AND NOT(child-[:CHILD]->()) " +
"RETURN child")
List<Category> findLeaves(String name);
2 回答
如果你想找到cypher 3.0中的所有 leaf nodes
http://console.neo4j.org/r/leaf-nodes
match (n)-[r]-() with n, count(r) as c where c = 1 return n
这是我用cypher找到的最简单的方法:http://console.neo4j.org/r/cgrndo
编辑:(因为人们最近提出了这个...这是使用3.x密码的更新)