简体   繁体   English

Neo4j,具有某些节点属性和关系名称的显示路径

[英]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: 在Neo4J代码示例( http://docs.neo4j.org/chunked/stable/tutorial-traversal-java-api.html )中,以下输出函数用于打印图形:

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. 输出函数显示包含节点和关系ID的ID。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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