2

I want to visualise data from Neo4j with the frontend-library D3.js in an Rails application, using Neo4jrb. For example I could use the following query to get my graph data.

query = "MATCH path = (a)-[b]->(c) RETURN path"
result = Neo4j::Session.current.query(query)

But this query is not giving me the exact data I want.

According to the Neo4j data visualisation guide there is a possibility to set the parameter resultDataContents to "graph". ( Neo4j documentation for "resultDataContents")

This is exactly what I need for my application. Is there any possibility to set this parameter in Neo4jrb, or another idea how to achieve such a result?

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
  • Not sure of ruby but I have written some raw Cypher query to retrieve nodes and relationships from neo4j (an old version), hope it helps: https://github.com/paradite/Graphpedia/blob/master/models/term.js#L476 – paradite May 20 '16 at 03:31

1 Answers1

1

Unfortunately not currently. The neo4j-core gem (which the neo4j gem uses) was build to abstract away the REST format. The "graph" format returns data in a different way.

You have a couple of options. You could make the JSON queries yourself or you could retrieve the nodes and relationships from the queries that you perform and then build your own nodes/relationships structure which is returned. This might be more future-proof anyway if you ever want to switch to Bolt.

A way that you might do this in your case:

query = "MATCH path = (a)-[b]->(c) RETURN nodes(path) AS nodes, rels(path) AS rels"
result = Neo4j::Session.current.query(query)
response = {nodes: [], rels: []}
result.each do |row|
  response[:nodes].concat(row.nodes)
  response[:rels].concat(row.rels)
end
response[:nodes].uniq!
response[:rels].uniq!
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • thanks, but I have one further problem with your idea. `nodes` are identified by their `uuid`. Inside of `rels` the start- and end-node are identified by `neo-id`, so it's not possible to match them (to use in D3 for example) ? – LimonChillo May 19 '16 at 22:18
  • You should be able to get the `neo_id` for nodes by calling the `neo_id` method – Brian Underwood May 20 '16 at 19:23
  • Thanks, I got a nice solution with your tips :) – LimonChillo May 23 '16 at 21:21