user1786283
user1786283

Reputation:

Neo4j Reordering a path

Is there anyway in Neo4j (maybe using PathExpander or RelationshipExpander) to reorder the path a traversal takes by the property of a relationship (in my case a timestamp) in java?

I have searched almost all the api's and community discussions, but cannot find a hint.

Upvotes: 2

Views: 345

Answers (1)

Lisa Li
Lisa Li

Reputation: 2592

You might create a path extender that extends a path according to the value of the property, something like this, (suppose you want an increasing order).

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

}

Then use your path extender in your traveral,

TraversalDescription td = Traversal.description()
        .breadthFirst()
        .expand(new OrderPathExpander(YourRelationshipType, Direction.INCOMING))
        .evaluator(new Evaluator() {...});

Upvotes: 4

Related Questions