【问题标题】:neo4j java traversal return multiple paths instead of oneneo4j java遍历返回多条路径而不是一条
【发布时间】:2015-02-06 11:08:17
【问题描述】:

我想遍历一个图并返回链接 2 个节点的所有路径,其中第一个关系是传出的,第二个是传入的。例如,如果关系已投票并且我想查看从节点 25 到节点 86 的所有可能路径,我有 MATCH p=((n {id:"25"}) -[*1..2]-> (m {id:"86"})) RETURN p; 然后我想检查在返回的路径中我是否在传出和传入关系中具有相同类型的属性(如果他们有相同的投票)。
我尝试使用 java 中的图形遍历 api 来实现这一点,但我得到的只是一条路径我怎样才能获得所有可能的路径以检查它们?

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

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);

【问题讨论】:

    标签: java path neo4j traversal


    【解决方案1】:

    遍历有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 );
    

    【讨论】:

    • 你说得对,我实际上需要关系唯一性 .uniqueness(Uniqueness.RELATIONSHIP_PATH ) 但是我怎样才能确定首先得到传出然后传入而不是 2 个传出或 2 个传入或传入传出?
    • 我的意思是我如何使用 traverse(Traverser.Order traversalOrder, StopEvaluator stopEvaluator,ReturnableEvaluator returnableEvaluator,RelationshipType firstRelationshipType,Direction firstDirection,RelationshipType secondRelationshipType,Direction secondDirection) 以获得我想要的这个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多