Paths { @UserFunction @Description("apoc.path.slice(path, [offset], [length]) - creates a sub-path with the given offset and length") public Path slice(@Name("path") Path path, @Name(value = "offset", defaultValue = "0") long offset,@Name(value = "length", defaultValue = "-1") long length) { if (path == null) return null; if (offset < 0) offset = 0; if (length == -1) length = path.length() - offset; if (offset == 0 && length >= path.length()) return path; Iterator<Node> nodes = path.nodes().iterator(); Iterator<Relationship> rels = path.relationships().iterator(); for (long i = 0; i < offset && nodes.hasNext() && rels.hasNext(); i++) { nodes.next(); rels.next(); } if (!nodes.hasNext()) return null; PathImpl.Builder builder = new PathImpl.Builder(nodes.next()); for (long i = 0; i < length && rels.hasNext(); i++) { builder = builder.push(rels.next()); } return builder.build(); }  @UserFunction @Description("apoc.path.create(startNode,[rels]) - creates a path instance of the given elements") Path create(@Name("startNode") Node startNode, @Name(value = "rels", defaultValue = "[]") List<Relationship> rels); @UserFunction @Description("apoc.path.slice(path, [offset], [length]) - creates a sub-path with the given offset and length") Path slice(@Name("path") Path path, @Name(value = "offset", defaultValue = "0") long offset,@Name(value = "length", defaultValue = "-1") long length); @UserFunction @Description("apoc.path.combine(path1, path2) - combines the paths into one if the connecting node matches") Path combine(@Name("first") Path first, @Name("second") Path second); @UserFunction @Description("apoc.path.elements(path) - returns a list of node-relationship-node-...") List<Object> elements(@Name("path") Path path);  }
@Test public void slice() throws Exception { TestUtil.testCall(db, "MATCH path = (a:A)-[:NEXT*]->(d:D) RETURN apoc.path.slice(path,1,1) as p",(row) -> assertPath(row, 1, "B", "C")); TestUtil.testCall(db, "MATCH path = (a:A)-[:NEXT*]->(d:D) RETURN apoc.path.slice(path,1) as p",(row) -> assertPath(row, 2, "B", "D")); TestUtil.testCall(db, "MATCH path = (a:A)-[:NEXT*]->(d:D) RETURN apoc.path.slice(path,1,100) as p",(row) -> assertPath(row, 2, "B", "D")); TestUtil.testCall(db, "MATCH path = (a:A)-[:NEXT*]->(d:D) RETURN apoc.path.slice(path,-1,0) as p",(row) -> assertPath(row, 0, "A", "A")); TestUtil.testCall(db, "MATCH path = (a:A)-[:NEXT*]->(d:D) RETURN apoc.path.slice(path,1,0) as p",(row) -> assertPath(row, 0, "B", "B")); TestUtil.testCall(db, "RETURN apoc.path.slice(null) as p",(row) -> assertNull(row.get("p"))); }