确定neo4j中两个节点之间的路径

时间:2013-05-04 14:48:47

标签: graph neo4j

我在图表中有两条路径:A-B-C-D和A-B-E-F。我想为这些路径分配识别号码,即A-B-C-D为1,A-B-E-F为2。

有可能吗?如果是,怎么样?

1 个答案:

答案 0 :(得分:1)

你的意思是一个持久的路径ID?这不是直接功能,但您可以在Cypher的查询中执行此操作。

如果你想要持久化,你总是可以使用索引,所以创建一个Relationship索引,它将路径1的Relationship存储在Path:1的键/值下。

编辑:获取更多信息后,这是一个使用索引的用例:

您可以在索引中对此进行定义。这是你做的:

    Node a = db.createNode();
    Node b = db.createNode();
    Node c = db.createNode();
    Node d = db.createNode();
    Node e = db.createNode();
    Node f = db.createNode();

    Relationship aTob = a.createRelationshipTo(b, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship bToc = b.createRelationshipTo(c, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship cTod = c.createRelationshipTo(d, DynamicRelationshipType.withName("RELATIONSHIP"));

    Relationship bToe = b.createRelationshipTo(e, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship eTof = e.createRelationshipTo(f, DynamicRelationshipType.withName("RELATIONSHIP"));

    Index<Relationship> relationshipIndex = db.index().forRelationships("PathIndex");
    String pathRId = UUID.randomUUID().toString();
    String pathMId = UUID.randomUUID().toString();

    relationshipIndex.add(aTob, "PathId", pathRId);
    relationshipIndex.add(bToc, "PathId", pathRId);
    relationshipIndex.add(cTod, "PathId", pathRId);

    relationshipIndex.add(aTob, "PathId", pathMId);
    relationshipIndex.add(bToe, "PathId", pathMId);
    relationshipIndex.add(eTof, "PathId", pathMId);

然后,当您想要查找路径时,您将按ID搜索。您将负责维护索引中的Set Id,这里我使用UUID,但您可以使用更具代表性的信息。从索引返回时,关系不会以任何可重复的顺序排列。