We have a project where we use Spring Data Neo4J. One of the important entities is shown below:
@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;
}
We have a requirement to find out all the leaf categories (that is, categories without any children) starting from a specific category whose name is known. For example, given the hierarchy shown below:
Electronics
Camera
Point and Shoot
SLR
Computing
Desktop
Laptop
Tablet
Netbook
Furniture
Tables
Office tables
Home tables
Chairs
Lounge chairs
Office chairs
a search for "Furniture" should return "Office tables", "Home tables", "Lounge chairs" and "Office chairs". Similarly, a search for "Computing" should return "Desktop", "Laptop", "Tablet" and "Netbook".
Need help in creating a cypher query that can be placed on a Spring Data repository method to give me all the leaf nodes starting from the specified node.
EDIT The following query (with the associated Spring Data repository method) worked after help from 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);