我的 graphDb 中有 3 种类型的节点。 技能
、SkillSubCluster
和SkillCluster
。一个 Skill
节点连接到一个或多个 SkillSubCluster
节点(一对多关系),一个 SkillSubCluster
节点连接到单个 SkillCluster
节点(1 对 1 关系)。
我想查找具有 skillCluster
名称的所有技能。我想出了这个密码查询 -
match(n:SkillCluster {CleanedText: "arts"}) match(n)<-[parent]-(x)<-[belongsTo]-(y) return y
节点的数量可能很大,所以我正在考虑返回分页结果。使用 skip
和 limit
运算符可以轻松完成此操作。另外,我想返回给定 skillCluster
节点的技能总数。
相应的cypher
查询将是
match(n:SkillCluster {CleanedText: "arts"}) match(n)<-[parent]-(x)<-[belongsTo]-(y) return count(y)
我正在尝试使用 neo4j-ogm for java 来做同样的事情。
我的技能等级是
public class Skill {
@Id @GeneratedValue
private Long id;
private String Name;
private String CleanedText;
@Relationship(type = "BelongsTo", direction = Relationship.OUTGOING)
private Set<SkillSubCluster> belongsTo = new HashSet<>();
}
其对应的DAO类
public class SkillDAO extends GenericDAO<Skill>{
public SkillDAO(Session session) {
super(session);
}
protected Class<Skill> getEntityType() {
return Skill.class;
}
}
和我的通用 DAO 类 -
public abstract class GenericDAO<T> {
private static final int DEPTH_LIST = 0;
private static final int DEPTH_ENTITY = 1;
private Session session;
public long filterCount(Iterable<Filter> filters){
return session.count(getEntityType(), filters);
}
public T find(Long id) {
return session.load(getEntityType(), id, DEPTH_ENTITY);
}
public T find(String name) {
return session.load(getEntityType(), name, DEPTH_ENTITY);
}
public void delete(Long id) {
session.delete(session.load(getEntityType(), id));
}
public void createOrUpdate(T entity) {
session.save(entity, DEPTH_ENTITY);
//return find(entity.id);
}
protected abstract Class<T> getEntityType();
public GenericDAO(Session session) {
this.session = session;
}
}
是否可以返回Skill
类以外的对象或获取复杂cypher
查询的结果,例如group by
等。
最佳答案
经过一段时间的研究,我想出了正确的方法。因此,在我的 GenericDAO 抽象类中,我必须添加以下方法 -
public abstract class GenericDAO<T> {
// Rest of the implementation from above
public Result runComplexQuery(String query){
return session.query(query, Collections.emptyMap());
}
// ..................
}
然后使用以下代码来获取计数-
public long getNodeCount(String clusterName){
query = "match(n:SkillCluster {CleanedText: \"" + clusterName.toLowerCase() + "\"}) "
+ "match(n)<-[parent]-(x)<-[belongsTo]-(y) return count(y) as numSkillNodes";
Iterable<Map<String, Object>> results = skillSubClusterDAO.runComplexQuery(query);
for(Map<String, Object> row: results){
count = (long) row.get("numSkillNodes");
System.out.println(count);
}
}
关于java - neo4j ogm,获取与特定查询匹配的节点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531086/