Neo4j重新排序路径



Neo4j中是否有任何东西(可能使用PathExpanderRelationshipExpander)可以根据java中关系的属性(在我的情况下是时间戳)对遍历路径进行重新排序?

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

您可以创建一个路径扩展程序,根据属性的值扩展路径,比如这样(假设您想要递增顺序)。

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() {...});

最新更新