TraversalDescription 遍历写法不同导致返回的结果不同,求解
使用Neo4j内嵌模式,想封装一套自己的接口,当封装到遍历的方法时,传入多个关系类型,就涉及到代码要分开拼接, 发现返回了所有有关联的节点,寻找问题时发现,不管是多个关系,还是一种关系,以下两种写法,返回结果不同。 未注释的代码,可以正确返回相应深度的节点;但是注释的代码,返回了所有关联的节点。
//请忽略自定义的数据类型GraphNode
public List<GraphNode> findNodesByRelationshipType(GraphNode startNode, GraphRelationshipType types, int depth){
GraphRelationshipType know = new GraphRelationshipType("friend");
try(Transaction tx = db.beginTx()){
TraversalDescription td = db.traversalDescription()
.breadthFirst()
.relationships(know)
.uniqueness(Uniqueness.NODE_GLOBAL)
.evaluator(Evaluators.atDepth(depth));
/*TraversalDescription td = db.traversalDescription();
td.breadthFirst();
td.relationships(know);
td.uniqueness(Uniqueness.NODE_GLOBAL);
td.evaluator(Evaluators.atDepth(depth));*/
Iterable<Node> nodes = td.traverse(startNode.getNode()).nodes();
if (nodes != null){
List<GraphNode> rtList = new ArrayList<GraphNode>();
for(Node n : nodes){
GraphNode node = new GraphNode();
node.setNode(n);
rtList.add(node);
}
return rtList;
}
}
return null;
}
public class GraphRelationshipType implements RelationshipType {
private String name;
public GraphRelationshipType(String typeName){
this.name = typeName;
}
[@Override](/user/Override)
public String name() {
return name;
}
}