I created a graph in Neo4j with 10 million nodes and 30 million relationships.
Each node is labeled as A (4 million nodes) , B (6 million nodes) or C (20 nodes).
Nodes in A lead to nodes in B. Nodes in B lead to other nodes in B, and to nodes in C.
For each node in A, I need to find the closest node (or nodes, if they are the same distance) in C, and add the ID of the C node as a value of a property in the A node.
Any help would be much appreciated.
Asked
Active
Viewed 209 times
0

dbank04
- 1
- 1
-
Welcome to SO! Please remember to include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Such as what you've tried so far, what failed, what research you did. – Johan Rin Jan 01 '19 at 12:45
1 Answers
0
So we're looking at a model like this (using :LEAD since you didn't specify a relationship type):
(:A)-[:LEAD]->(:B)
(:B)-[:LEAD]->(:B)
(:B)-[:LEAD]->(:C)
APOC Procedures offers the best solution for this one, but it's a two-parter since we first find the closest :C node using the path expander procedures, then rematch using that distance to get the full collection of :C nodes reachable at that distance.
You'll also want to make use of apoc.periodic.iterate() so you can batch this, though you may want to play around with the batchSize.
I'm making some assumptions in this query since you didn't provide much in the way of properties to use in the graph.
CALL apoc.periodic.iterate("MATCH (a:A) RETURN a",
"CALL apoc.path.spanningTree(a, {relationshipFilter:'LEAD>', labelFilter:'/C', limit:1}) YIELD path
WITH a, length(path) as length
CALL apoc.path.subgraphNodes(a, {relationshipFilter:'LEAD>', labelFilter:'/C', maxLevel:length}) YIELD node
WITH a, collect(node.id) as ids
SET a.cIDs = ids",
{batchSize:1000}) YIELD batches, total, errorMessages
RETURN batches, total, errorMessages

InverseFalcon
- 29,576
- 4
- 38
- 51
-
Thank you so much! I refined it a little bit by specifying the relationship types, and used {batchSize:10000, iterateList:true, parallel:true}. Worked like a charm and completed within a few minutes. – dbank04 Jan 02 '19 at 10:43