Reputation: 1
I am using Neo4j
for graph database, and using Java to extract the path from it.
I have a path as follows:
[(3)--[KNOWS,5]-->(4)--[KNOWS,6]-->(5)--[KNOWS,9]-->(6)--[KNOWS,10]-->(7)--[KNOWS,12]-->(9)]
I want to replace the node ids in the path with their property.
ex. For node id 3 with property "name=ABC"
, output should come like "[(ABC)--[KNOWS,5]....]"
How it can be done?
Upvotes: 0
Views: 99
Reputation: 41676
For Cypher you can use extract:
START n=node:index(key="value")
MATCH path=n-[:KNOWS*..5]-m
RETURN extract(x in nodes(path) : x.name)
if you want to keep the relationships, you can do:
RETURN extract(x in path : coalesce(x.name?,ID(x)+":"+type(x)))
see: http://console.neo4j.org/r/zvmyz
Upvotes: 1
Reputation: 39915
I assume the output we've shown is Path.toString(). There is no direct way to configure a different way of output. However you can easily iterate over the path and create your own string representation of a Path:
String myCustomPathToString(Path path) {
StringBuilder sb = new StringBuilder();
Node lastNode = null;
for (PropertyContainer propertyContainer: path) {
if (propertyContainer instanceof Node) {
lastNode = (Node)propertyContainer
Object default = lastNode.getId(); // as fallback
sb.append("(").append(propertyContainer.getProperty("name", default).append(")");
}
if (propertyContainer instanceof Relationship) {
Relationship rel = (Relationship)propertyContainer ;
Object id = rel.getId();
if (rel.getEndNode().equals(lastNode)) sb.append("<");
sb.append("--[").append(rel.getType().name()).append(",").append(id).append("]--");
if (rel.getStartNode().equals(lastNode)) sb.append(">");
}
}
return sb.toString();
}
DISCLAIMER: I did not test the snippet above, so it's more a guidline than a working example.
Upvotes: 2