use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.
the class GraphRefactoring method mergeNodes.
/**
* Merges the nodes onto the first node.
* The other nodes are deleted and their relationships moved onto that first node.
*/
@Procedure(mode = Mode.WRITE)
@Description("apoc.refactor.mergeNodes([node1,node2],[{properties:'override' or 'discard' or 'combine'}]) merge nodes onto first in list")
public Stream<NodeResult> mergeNodes(@Name("nodes") List<Node> nodes, @Name(value = "config", defaultValue = "") Map<String, Object> config) {
if (nodes == null || nodes.isEmpty())
return Stream.empty();
// grab write locks upfront consistently ordered
try (Transaction tx = db.beginTx()) {
nodes.stream().distinct().sorted(Comparator.comparingLong(Node::getId)).forEach(tx::acquireWriteLock);
tx.success();
}
RefactorConfig conf = new RefactorConfig(config);
final Node first = nodes.get(0);
nodes.stream().skip(1).distinct().forEach(node -> mergeNodes(node, first, true, conf));
return Stream.of(new NodeResult(first));
}
use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.
the class SubgraphTest method testOptionalSubgraphWithResultsShouldYieldExpectedResults.
@Test
public void testOptionalSubgraphWithResultsShouldYieldExpectedResults() throws Throwable {
String controlQuery = "MATCH (m:Movie {title: 'The Matrix'})-[*0..3]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
List<NodeResult> subgraph;
try (Transaction tx = db.beginTx()) {
Result result = db.execute(controlQuery);
subgraph = (List<NodeResult>) result.next().get("subgraph");
}
String query = "MATCH (m:Movie {title: 'The Matrix'}) CALL apoc.path.subgraphNodes(m,{maxLevel:3, optional:true}) yield node return COLLECT(node) as subgraphNodes";
TestUtil.testCall(db, query, (row) -> {
List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
assertEquals(subgraph.size(), subgraphNodes.size());
assertTrue(subgraph.containsAll(subgraphNodes));
});
}
use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.
the class SubgraphTest method testSubgraphWithRelationshipFilterShouldContainExpectedNodes.
@Test
public void testSubgraphWithRelationshipFilterShouldContainExpectedNodes() throws Throwable {
String controlQuery = "MATCH path = (:Person {name: 'Keanu Reeves'})-[:ACTED_IN*0..3]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
List<NodeResult> subgraph;
try (Transaction tx = db.beginTx()) {
Result result = db.execute(controlQuery);
subgraph = (List<NodeResult>) result.next().get("subgraph");
}
String query = "MATCH (k:Person {name: 'Keanu Reeves'}) CALL apoc.path.subgraphNodes(k,{maxLevel:3, relationshipFilter:'ACTED_IN'}) yield node return COLLECT(node) as subgraphNodes";
TestUtil.testCall(db, query, (row) -> {
List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
assertEquals(subgraph.size(), subgraphNodes.size());
assertTrue(subgraph.containsAll(subgraphNodes));
});
}
use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.
the class SubgraphTest method testSubgraphAllShouldContainExpectedNodesAndRels.
@Test
public void testSubgraphAllShouldContainExpectedNodesAndRels() throws Throwable {
String controlQuery = "MATCH path = (:Person {name: 'Keanu Reeves'})-[*0..3]-(subgraphNode) " + "with collect(distinct subgraphNode) as subgraph " + "call apoc.algo.cover([node in subgraph | id(node)]) yield rel " + "return subgraph, collect(rel) as relationships";
final List<NodeResult> subgraph;
final List<RelationshipResult> relationships;
try (Transaction tx = db.beginTx()) {
Result result = db.execute(controlQuery);
Map<String, Object> row = result.next();
subgraph = (List<NodeResult>) row.get("subgraph");
relationships = (List<RelationshipResult>) row.get("relationships");
}
String query = "MATCH (k:Person {name: 'Keanu Reeves'}) " + "CALL apoc.path.subgraphAll(k,{maxLevel:3}) yield nodes, relationships " + "return nodes as subgraphNodes, relationships as subgraphRelationships";
TestUtil.testCall(db, query, (row) -> {
List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
List<RelationshipResult> subgraphRelationships = (List<RelationshipResult>) row.get("subgraphRelationships");
assertEquals(subgraph.size(), subgraphNodes.size());
assertTrue(subgraph.containsAll(subgraphNodes));
assertEquals(relationships.size(), subgraphRelationships.size());
assertTrue(relationships.containsAll(subgraphRelationships));
});
}
use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.
the class SubgraphTest method testSpanningTreeShouldHaveOnlyOnePathToEachNode.
@Test
public void testSpanningTreeShouldHaveOnlyOnePathToEachNode() throws Throwable {
String controlQuery = "MATCH (m:Movie {title: 'The Matrix'})-[*0..4]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
List<NodeResult> subgraph;
try (Transaction tx = db.beginTx()) {
Result result = db.execute(controlQuery);
subgraph = (List<NodeResult>) result.next().get("subgraph");
}
String query = "MATCH (m:Movie {title: 'The Matrix'}) " + "CALL apoc.path.spanningTree(m,{maxLevel:4}) yield path " + "with collect(path) as paths " + "with paths, size(paths) as pathCount " + "unwind paths as path " + "with pathCount, collect(distinct last(nodes(path))) as subgraphNodes " + "return pathCount, subgraphNodes";
TestUtil.testCall(db, query, (row) -> {
List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
long pathCount = (Long) row.get("pathCount");
assertEquals(subgraph.size(), subgraphNodes.size());
assertTrue(subgraph.containsAll(subgraphNodes));
// assert every node has a single path to that node - no cycles
assertEquals(pathCount, subgraphNodes.size());
});
}
Aggregations