Skip to content

Commit

Permalink
[7nVhdJu3] apoc.convert.toTree keep all rels in tree fix (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
gem-neo4j authored Feb 19, 2024
1 parent 8222da0 commit 3ebb49f
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 91 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/apoc/convert/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,12 @@ public List<Object> fromJsonList(
}

@Procedure("apoc.convert.toTree")
@Description(
"Returns a stream of `MAP` values, representing the given `PATH` values as a tree with at least one root.")
// todo optinally provide root node
@Description("Returns a stream of `MAP` values, representing the given `PATH` values as a tree with at least one root.")
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()));
if (paths == null || 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();
Expand All @@ -203,8 +201,12 @@ public Stream<MapResult> toTree(
// 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);
// Check that this combination of rel and node doesn't already exist
Optional<Map<String, Object>> optMap = list.stream()
.filter(elem -> elem.get("_id").equals(m.getId()))
.filter(elem ->
elem.get("_elementId").equals(m.getElementId())
&& elem.get(typeName + "._elementId").equals(r.getElementId())
)
.findFirst();
if (!optMap.isPresent()) {
Map<String, Object> mMap = toMap(m, nodes);
Expand Down Expand Up @@ -255,9 +257,10 @@ public String toSortedJsonMap(

private Map<String, Object> addRelProperties(
Map<String, Object> mMap, String typeName, Relationship r, Map<String, List<String>> relFilters) {
String prefix = typeName + ".";
mMap.put(prefix + "_elementId", r.getElementId());
Map<String, Object> rProps = r.getAllProperties();
if (rProps.isEmpty()) return mMap;
String prefix = typeName + ".";
if (relFilters.containsKey(typeName)) {
rProps = filterProperties(rProps, relFilters.get(typeName));
}
Expand All @@ -269,7 +272,8 @@ private Map<String, Object> toMap(Node n, Map<String, List<String>> nodeFilters)
Map<String, Object> props = n.getAllProperties();
Map<String, Object> result = new LinkedHashMap<>(props.size() + 2);
String type = Util.labelString(n);
result.put("_id", n.getId());
result.put("_id", n.getId());;
result.put("_elementId", n.getElementId());
result.put("_type", type);
if (nodeFilters.containsKey(type)) { // Check if list contains LABEL
props = filterProperties(props, nodeFilters.get(type));
Expand Down
Loading

0 comments on commit 3ebb49f

Please sign in to comment.