JustThatGuy93
JustThatGuy93

Reputation: 121

Format Cypher Query for D3 Graph

I've got a database holding information for just 1 movie currently. I'm trying to write a query that would return the nodes and links in a formatted way.

This is what my query is currently... CALL apoc.export.json.query("MATCH (n) RETURN n LIMIT 6", "file:////limit.json", {})

Currently, this is what the query is returning...

{"n":{"type":"node","id":"0","labels":["Actor"],"properties":{"name":"Chris Pratt"}}}
{"n":{"type":"node","id":"1","labels":["Movie"],"properties":{"actors":["Chris Pratt","Vin Diesel","Bradley Cooper","Zoe Saldana"],"revenue":333.13,"year":2014.0,"genres":["Action","Adventure","Sci-Fi"],"director":"James Gunn","rating":8.1,"name":"Guardians of the Galaxy","runtime":121.0,"description":"A group of intergalactic criminals are forced to work together to stop a fanatical warrior from taking control of the universe.","rank":1.0,"votes":757074.0,"metascore":76.0}}}
{"n":{"type":"node","id":"2","labels":["Actor"],"properties":{"name":"Vin Diesel"}}}
{"n":{"type":"node","id":"3","labels":["Actor"],"properties":{"name":"Bradley Cooper"}}}
{"n":{"type":"node","id":"4","labels":["Actor"],"properties":{"name":"Zoe Saldana"}}}
{"n":{"type":"node","id":"5","labels":["Director"],"properties":{"name":"James Gunn"}}}

Whereas I'd like to have it formatted into something like this...

{
  "nodes": [
    {
      "id": "Guardians of the Galaxy",
      "revenue": 333.13,
      "year": 2014,
      "genre": ["Action","Adventure","Sci-Fi"]
      "director": "James Gunn",
      "rating": 8.1,
      "runtime": 121
    },
    {
      "id": "Chris Pratt"
    },
    {
      "id": "Vin Diesel"
    },
    {
      "id": "Bradley Cooper"
    },
    {
      "id": "Zoe Saldana"
    }
  ],
  "links": [
    {
      "source": "Guardians of the Galaxy",
      "target": "Chris Pratt"
    },
    {
      "source": "Guardians of the Galaxy",
      "target": "Vin Diesel"
    },
    {
      "source": "Guardians of the Galaxy",
      "target": "Bradley Cooper"
    },
    {
      "source": "Guardians of the Galaxy",
      "target": "Zoe Saldana"
    }
  ]
}

I know Neo4j just updated how it's handling parameters, but I'm still unsure how they would allow for me to format my output as such so that it can be read as is for a D3 Graph.

Upvotes: -1

Views: 308

Answers (2)

Jeff Arnold
Jeff Arnold

Reputation: 123

You can accomplish this with two queries, one for nodes and one for links. You do not need apoc.

Query for all nodes:

MATCH (n) return n{.id, .revenue, .year, .genre, .director, .rating, .runtime} as nodes

Query for all relationships (links):

MATCH (n)-[r]->(m) return r{source: startnode(r).id, target: endnode(r).id} as links

Then the results of these queries go into your D3 object:

const nodes = <run query to get nodes>
const links = <run query to get links>
const d3Object = {nodes, links}

Ideally someone has a one-query answer to this problem, but the multiple query approach is at least clean and easy to read.

Upvotes: 2

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6534

This could be done with the following query:

MATCH (n)
RETURN {id:coalesce(n.name, n.title), year: n.year} as result
UNION
MATCH (n)-->(m)
RETURN {source: coalesce(n.name, n.title), target: coalesce(m.name, m.title)} as result

You can obviously add additional node properties as you like, but this should get you started. You can also use multiple UNION queries and specify the node properties for each node label separately.

Upvotes: 2

Related Questions