Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document apoc.convert.toTree #1269

Closed
jexp opened this issue Aug 5, 2019 · 3 comments · Fixed by #2036
Closed

Document apoc.convert.toTree #1269

jexp opened this issue Aug 5, 2019 · 3 comments · Fixed by #2036
Assignees
Labels

Comments

@jexp
Copy link
Member

jexp commented Aug 5, 2019

Especially the config options

  • lower case relationships
  • exclusion / inclusion filters for properties by label/type

perhaps also how to convert individual nodes/rels into paths using the paths functions

and if/how virtual entities work with this procedure

@fabio-s-franco
Copy link

The documenation for apoc.toTree is completely absent in conversion functions of the documentation. I am trying to figure out how to include property of relationships in the result, but the documentation is not there. So I don't even know if it's possible.

@bburns
Copy link

bburns commented Aug 26, 2020

FWIW here's the current code -

@Procedure("apoc.convert.toTree")
@Description("apoc.convert.toTree([paths],[lowerCaseRels=true], [config]) creates a stream of nested documents representing the at least one root of these paths")
// todo optinally provide root node
public Stream<MapResult> toTree(@Name("paths") List<Path> paths, @Name(value = "lowerCaseRels",defaultValue = "true") boolean lowerCaseRels, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
if (paths.isEmpty()) return Stream.of(new MapResult(Collections.emptyMap()));
ConvertConfig conf = new ConvertConfig(config);
Map<String, List<String>> nodes = conf.getNodes();
Map<String, List<String>> rels = conf.getRels();
Map<Long, Map<String, Object>> maps = new HashMap<>(paths.size() * 100);
for (Path path : paths) {
Iterator<Entity> it = path.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
Map<String, Object> nMap = maps.computeIfAbsent(n.getId(), (id) -> toMap(n, nodes));
if (it.hasNext()) {
Relationship r = (Relationship) it.next();
Node m = r.getOtherNode(n);
String typeName = lowerCaseRels ? r.getType().name().toLowerCase() : r.getType().name();
// todo take direction into account and create collection into outgoing direction ??
// parent-[:HAS_CHILD]->(child) vs. (parent)<-[:PARENT_OF]-(child)
if (!nMap.containsKey(typeName)) nMap.put(typeName, new ArrayList<>(16));
List<Map<String, Object>> list = (List) nMap.get(typeName);
Optional<Map<String, Object>> optMap = list.stream()
.filter(elem -> elem.get("_id").equals(m.getId()))
.findFirst();
if (!optMap.isPresent()) {
Map<String, Object> mMap = toMap(m, nodes);
mMap = addRelProperties(mMap, typeName, r, rels);
maps.put(m.getId(), mMap);
list.add(maps.get(m.getId()));
}
}
}
}
return paths.stream()
.map(Path::startNode)
.distinct()
.map(n -> maps.remove(n.getId()))
.map(m -> m == null ? Collections.<String,Object>emptyMap() : m)
.map(MapResult::new);
}

and tests - https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/16581f3253f82e75506428dfd2a498346c6bfb1d/core/src/test/java/apoc/convert/ConvertJsonTest.java

The tests include some examples of including/excluding properties - eg

CALL apoc.convert.toTree(ps, true, {nodes: {Category: ['name']}, rels: {subcategory:['-id']}}) yield value

@jexp
Copy link
Member Author

jexp commented Aug 26, 2020

@conker84 @mneedham can one of you add docs with these examples?

vga91 added a commit to vga91/neo4j-apoc-procedures that referenced this issue Jun 25, 2021
fbiville pushed a commit that referenced this issue Jul 16, 2021
github-actions bot pushed a commit that referenced this issue Jul 16, 2021
github-actions bot pushed a commit that referenced this issue Jul 16, 2021
conker84 pushed a commit that referenced this issue Jul 20, 2021
Fixes #1269

Co-authored-by: Giuseppe Villani <[email protected]>
conker84 pushed a commit that referenced this issue Jul 20, 2021
github-actions bot added a commit that referenced this issue Jul 20, 2021
Fixes #1269

Co-authored-by: Giuseppe Villani <[email protected]>
conker84 pushed a commit that referenced this issue Jul 20, 2021
Fixes #1269

Co-authored-by: Giuseppe Villani <[email protected]>

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Giuseppe Villani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants