简体   繁体   中英

Neo4j, Display Paths with Certain Node Attributes and Relationship Names

In the Neo4J code examples ( http://docs.neo4j.org/chunked/stable/tutorial-traversal-java-api.html ) following output function is used to print out graphs:

for ( Path position : db.traversalDescription()
        .depthFirst()
        .relationships( Rels.KNOWS )
        .relationships( Rels.LIKES, Direction.INCOMING )
        .evaluator( Evaluators.toDepth( 5 ) )
        .traverse( node ) )
{
    output += position + "\n";
}

The output function displays node and relationship IDs incl. relationship names:

(6)
(6)<--[LIKES,1]--(3)
(6)<--[LIKES,1]--(3)--[KNOWS,6]-->(0)

How can I display certain node and relationship attributes (in this example names and relationship types only)? I would like to have following output:

Joe
Joe – [likes] – Lisa
Joe – [likes] – Lisa – [knows] Lars

Thanks in advance

You have to write your own formatter, that is configured with the properties you want to show:

toString(Node n, String prop) { return "("+node.getProperty(prop)+")"; }
toString(Relationship r) { return "-["+r.getType().name()+"]->"; }

and the iterate over the path, choosing one or another

public String render(Path path, String prop) {
   StringBuilder result=new StringBuilder();
   for (PropertyContainer pc : path) {
      if (pc instanceof Node) sb.append(toString((Node)pc,prop));
      else sb.append(toString((Relationship)pc));
   }
   return sb.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM