2

Neo4j 中是否无论如何(可能使用PathExpanderor RelationshipExpander)通过java中关系的属性(在我的情况下为时间戳)重新排序遍历所采用的路径?

我搜索了几乎所有的 api 和社区讨论,但找不到提示。

4

1 回答 1

4

您可以创建一个路径扩展器,根据属性的值扩展路径,如下所示(假设您想要一个递增的顺序)。

public class OrderPathExpander implements PathExpander<String> {

private final RelationshipType relationshipType;
private final Direction direction;

public OrderPathExpander( RelationshipType relationshipType, Direction direction )
{
    this.relationshipType = relationshipType;
    this.direction = direction;
}
@Override
public Iterable<Relationship> expand(Path path, BranchState<String> state)
{
    List<Relationship> results = new ArrayList<Relationship>();
    if ( path.length() == 0 ) {
        for ( Relationship r : path.endNode().getRelationships( relationshipType, direction ) )
        {
                results.add( r );

        }

    }
    else {
    for ( Relationship r : path.endNode().getRelationships( relationshipType, direction ) )
    {
        if ( r.getProperty("timestamp") >= (path.lastRelationship().getProperty("timestamp"))  )
        {
            results.add( r );
        }
    }
    }
    return results;
}
@Override
public PathExpander<String> reverse()
{
    return null;
}

}

然后在你的旅行中使用你的路径扩展器,

TraversalDescription td = Traversal.description()
        .breadthFirst()
        .expand(new OrderPathExpander(YourRelationshipType, Direction.INCOMING))
        .evaluator(new Evaluator() {...});
于 2013-08-20T15:46:32.607 回答