首页 文章

neo4j java遍历返回多个路径而不是一个

提问于
浏览
1

我想遍历一个图并返回链接2个节点的所有路径,其中第一个关系是传出的,第二个是传入的 . 例如,如果关系是Voted并且我想要查看从节点25到节点86的所有可能路径,那么我有 MATCH p=((n {id:"25"}) -[*1..2]-> (m {id:"86"})) RETURN p; 然后我想检查在返回路径中我是否在传出和传入关系中具有相同类型的属性(如果它们有相同的投票) .
我尝试使用java中的图遍历api实现这一点,但我回来的只是一条路径 how can I get all possible paths in order to check them?

{它基本上是与所有常见邻居问题的检查关系}

int common = 0;
    int  diff = 0;
    for ( Path position : graphDb.traversalDescription()
            .relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING)
            .relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING)
           // .evaluator(Evaluators.fromDepth(1))
            .evaluator(Evaluators.toDepth(2))
            .evaluator(Evaluators.includeWhereEndNodeIs(node2))
            // .evaluator(Evaluators.excludeStartPosition())
            .traverse(node1))
             {
        Iterable<Relationship> myRels = position.reverseRelationships();

        for (Relationship temp : myRels) {
            System.out.println((temp.getStartNode()).getProperty("id") + " with " + temp.getProperty("with") + " :" + (temp.getEndNode()).getProperty("id"));
        }
           String with = "";
                    int i = 0;

        for (Relationship temp : myRels) {
                        if (i == 0) {
                            with = (String) temp.getProperty("with");
                            i++;
                        }
                        if (i == 1) {
                            if (((String) temp.getProperty("with")).equals(with)) {
                                common++;
                            } else {
                                diff++;
                            }
                        }
                    }
    }
    return (double) common * 100 / (common + diff);

1 回答

  • 2

    遍历有uniqueness rules . 阅读该链接,它讨论了遍历器的工作原理以及如何配置它 . 默认情况下,唯一性规则设置为NODE_GLOBAL,这意味着某个节点不能多次遍历 .

    我怀疑这可能是你的问题;如果您正在寻找一个目标节点,但想要到该节点的所有路径,则应使用 RELATIONSHIP_GLOBAL ,或文档中列出的其他选项之一 . 您的遍历器正在寻找一个端节点,默认情况下,您只能遍历该节点一次 .

    因此,要尝试此修复,请为遍历描述添加不同的唯一性:

    graphDb.traversalDescription()
       .relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING)
       .relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING)
       .uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
    

相关问题