如何遍历第一邻居及其关系 [英] how to traverse first neighbors and their relationships
问题描述
我想这可以轻松实现,但无法解决.是否可以在一次遍历(Neo4j 1.9RC2)中实现以下目标: 从节点开始,包括其所有第一个邻居(深度1),并包括其邻居之间的所有链接(如果有).方向无关紧要.
I guess this can be trivially achieved but can't figure it out. Is it possible to achieve the following in one traversal (Neo4j 1.9RC2): starting from a node, include all its first neighbors (depth 1) and include all links between it's neighbors (if any). The direction is irrelevant.
这是一个测试场景:
+-+ +-+ +-+
|7+----->5| |4+------+
+++ +-+-----------------+-+ |
| | | |
| | +--+ | |
| +------|1 |-------+ |
++> +-++ +-+ +-+
|8| | |2+-----|6|
+-+ +----------------+++ +-+
|
|
+-+
|3|
+-+
从节点1开始,我想包括节点2,4和5以及关系2-4和4-5,但不包括2-6或5-7. 和测试治具:
starting from node 1, i want to include nodes 2,4 and 5 and relationships 2-4 and 4-5, but not 2-6 or 5-7. And the test fixture:
Node[] nodes = new Node[10];
Transaction tx = graphDb.beginTx();
try {
for (int i = 1; i < 10; i++) {
Node node = graphDb.createNode();
node.setProperty("id", i);
otuIdIndex.add(node, "id", i);
nodes[i] = node;//nodes[0] is empty!
}
nodes[1].createRelationshipTo(nodes[2], RelTypes.CONNECTED_TO);
nodes[1].createRelationshipTo(nodes[4], RelTypes.CONNECTED_TO);
nodes[1].createRelationshipTo(nodes[5], RelTypes.CONNECTED_TO);
nodes[2].createRelationshipTo(nodes[4], RelTypes.CONNECTED_TO);
nodes[2].createRelationshipTo(nodes[6], RelTypes.CONNECTED_TO);
nodes[3].createRelationshipTo(nodes[2], RelTypes.CONNECTED_TO);
nodes[5].createRelationshipTo(nodes[4], RelTypes.CONNECTED_TO);
nodes[7].createRelationshipTo(nodes[5], RelTypes.CONNECTED_TO);
nodes[7].createRelationshipTo(nodes[8], RelTypes.CONNECTED_TO);
tx.success();
} finally {
tx.finish();
}
final TraversalDescription traversalDescription = Traversal.description().breadthFirst()
.relationships(RelTypes.CONNECTED_TO, Direction.BOTH)
.uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
.evaluator(Evaluators.toDepth(2))
.evaluator(Evaluators.excludeStartPosition());
for (Path path : traversalDescription.traverse(nodes[1])) {
System.out.println(path);
}
输出为:
(1)--[CONNECTED_TO,0]-->(2)
(1)--[CONNECTED_TO,1]-->(4)
(1)--[CONNECTED_TO,2]-->(5)
(1)--[CONNECTED_TO,0]-->(2)--[CONNECTED_TO,3]-->(4)
(1)--[CONNECTED_TO,0]-->(2)--[CONNECTED_TO,4]-->(6)
(1)--[CONNECTED_TO,0]-->(2)<--[CONNECTED_TO,5]--(3)
(1)--[CONNECTED_TO,1]-->(4)<--[CONNECTED_TO,6]--(5)
(1)--[CONNECTED_TO,2]-->(5)<--[CONNECTED_TO,7]--(7)
我想做的是排除这三个:
and what I'm trying to do is to exclude these three:
(1)--[CONNECTED_TO,0]-->(2)<--[CONNECTED_TO,5]--(3)
(1)--[CONNECTED_TO,0]-->(2)--[CONNECTED_TO,4]-->(6)
(1)--[CONNECTED_TO,2]-->(5)<--[CONNECTED_TO,6]--(7)
Lasse 建议以下密码查询我需要做什么,但是我想知道这是否可以通过Traversal
来实现.
which sort of does what I need but I'm wondering if this is doable with Traversal
.
推荐答案
好,找到了一种方法,但是它太慢了,在200K节点/700K关系数据库中,每秒加载一个网络的时间,而fromDepth(1).toDepth(1)评估程序的加载时间为0.006秒(150倍):
Ok, found one way of doing this, but it's way too slow, in a 200K nodes/700K relationships db it takes a second to load one network compared to 0.006sec for fromDepth(1).toDepth(1) evaluator (150 times factor):
final TraversalDescription traversalDescription = Traversal.description().breadthFirst()
.relationships(RelTypes.CONNECTED_TO, Direction.BOTH)
.uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
.evaluator(Evaluators.includeIfAcceptedByAny(new PathEvaluator() {
private final Set<Long> firstNeighbors = new HashSet<Long>();
@Override
public Evaluation evaluate(Path path, BranchState state) {
if (path.length() == 0) {
return Evaluation.EXCLUDE_AND_CONTINUE;
} else if (path.length() == 1) {
firstNeighbors.add(path.endNode().getId());
return Evaluation.INCLUDE_AND_CONTINUE;
} else if (path.length() == 2) {
final Iterator<Node> iterator = path.nodes().iterator();
iterator.next();//start node, just skip
Node firstNeighbor = iterator.next();
if (firstNeighbors.contains(path.endNode().getId()) && firstNeighbors.contains(firstNeighbor.getId())) {
return Evaluation.INCLUDE_AND_CONTINUE;
} else {
return Evaluation.EXCLUDE_AND_CONTINUE;
}
} else {
return Evaluation.EXCLUDE_AND_PRUNE;
}
}
@Override
public Evaluation evaluate(Path path) {
return evaluate(path, null);
}
}));
更新:暴风雨建议使用
这篇关于如何遍历第一邻居及其关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!