Greetings,
Apologies if info around this topic exists. I'm using Neo4j Community 3.5.1 w/ apoc-3.5.0.3-all.
Goal
Here is what I am trying to achieve... I want to run a cypher query that outputs a JSON file that is in the format that is consumable/readable by D3.js to display a Sankey Diagram. This site displays the D3 source files. Here is the JSON formatted structure I am hoping apoc can output that D3 requires...
{
"nodes":[
{"node":0,"name":"node0"},
{"node":1,"name":"node1"},
{"node":2,"name":"node2"},
{"node":3,"name":"node3"},
{"node":4,"name":"node4"}
],
"links":[
{"source":0,"target":2,"value":2},
{"source":1,"target":2,"value":2},
{"source":1,"target":3,"value":2},
{"source":0,"target":4,"value":2},
{"source":2,"target":3,"value":2},
{"source":2,"target":4,"value":2},
{"source":3,"target":4,"value":4}
]}
This is what the properly formatted json file in D3 Sankey will look like...
What I'm trying
I've got a working cypher query that returns Source Target Value, but I don't know how to add to the query so that it first groups all of the "nodes" (and numbers the nodes starting at 0) and then groups all of the "links" as seen above.
With "MATCH (bry:Brewery)-[r:BREWS]->(b:Beer)<-[r2:IS_BARREL_AGED]-(ba:BarrelAged)
With bry as brewery, size(collect(b.name)) as number
Where number > 1
Match (brewery)-[:BREWS]->(b2:Beer)<-[:IS_BARREL_AGED]-(ba2:BarrelAged)
With brewery, ba2, count(b2) as cnt
Return brewery.name as source, ba2.name as target, cnt as value Order by target ASC
UNION All
MATCH (bry:Brewery)-[r:BREWS]->(b:Beer)<-[r2:IS_BARREL_AGED]-(ba:BarrelAged)
With bry as brewery, size(collect(b.name)) as number
Where number > 1
Match (brewery)-[:BREWS]->(b2:Beer)<-[:IS_BARREL_AGED]-(ba2:BarrelAged)
Optional Match (brewery)-[:BREWS]->(b2)-[:IS_A]-(bt:BeerType)
With brewery, ba2, count(b2) as cnt, bt
Return ba2.name as source, bt.name as target, cnt as value Order by source ASC"
as query
CALL apoc.export.json.query(query, "/filelocation/barrelsankey.json")
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
Does anyone have a suggestion as to what I need to do to my query to make its JSON output the same structure required of D3 Sankey?