cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to get Data for All Nodes From a Query in JavaScript?

Hi. So I am trying to develop a front-end on my own using JavaScript and G6 (Graph visualization library). I have the following query which returns all the nodes that are connected to a source node:

MATCH (n:Account{name:"Acc A"})-[*]-(connected) RETURN n,connected

The above query will give me all the connected nodes in that specific chain as shown below:
3X_1_8_18a465b8944dd19ac09e5bd4f4bf9564d799c4b4.png

However, the problem with this is in the JSON response from the server, it doesn't return any information on the relationship (which node is connected to which node, relationship label, properties, etc) so I'm not actually able to visualize the nodes along with their relationships.

Am I doing something wrong here? Or is there another way to get the data on the nodes and relationships?

1 ACCEPTED SOLUTION

Hi @nayeerm752 ,

A few observations...

In your cypher query, you are MATCHing a pattern using any length, which is ok for small graphs but could be huge and take a lot of time for large graphs. Consider using an upper bounds like [*5] to limit the "neighborhood" around the source node.

In your RETURN clause, you're asking for just n, connected which will only return the source node and any reachable nodes in the neighborhood. When you run this query in Neo4j Browser, you may get a different visual result because the visualization automatically "fills in the gaps", which is usually what people want to see but is not an accurate reflection of the query results. If you switch to the tabular view, you would notice that the actual result set does not have relationships.

When you know you want all the graph elements, return paths instead. To do that you assign a variable to the pattern and then return that variable.

Putting all that together, try this:

MATCH p=(n:Account{name:"Acc A"})-[*5]-() RETURN p

When processing the results in javascript, each row will now be a Path which contains Nodes and Relationships. Keep in mind that across all the returned paths many nodes and relationships will be repeated, so there may be a bit of de-duplication necessary.

Best,
ABK

View solution in original post

4 REPLIES 4

Hi @nayeerm752 ,

A few observations...

In your cypher query, you are MATCHing a pattern using any length, which is ok for small graphs but could be huge and take a lot of time for large graphs. Consider using an upper bounds like [*5] to limit the "neighborhood" around the source node.

In your RETURN clause, you're asking for just n, connected which will only return the source node and any reachable nodes in the neighborhood. When you run this query in Neo4j Browser, you may get a different visual result because the visualization automatically "fills in the gaps", which is usually what people want to see but is not an accurate reflection of the query results. If you switch to the tabular view, you would notice that the actual result set does not have relationships.

When you know you want all the graph elements, return paths instead. To do that you assign a variable to the pattern and then return that variable.

Putting all that together, try this:

MATCH p=(n:Account{name:"Acc A"})-[*5]-() RETURN p

When processing the results in javascript, each row will now be a Path which contains Nodes and Relationships. Keep in mind that across all the returned paths many nodes and relationships will be repeated, so there may be a bit of de-duplication necessary.

Best,
ABK

Hi @abk ,

I tried the query as you mentioned, but it doesn't seem to send anything in its response:


Any idea why there isn't anything being returned in its response.

The "variable length relationship" is specifying an exact length of '5'.

You could:

  1. Specify an exact length of 5: MATCH p=(start)-[*5]-(end) RETURN p
  2. Specify an upper bounds of 5: MATCH p=(start)-[*..5]-(end) RETURN p
  3. Specify a lower bound of 5: MATCH p=(start)-[*5..]-(end) RETURN p
  4. Specify a range of 3 to 5: MATCH p=(start)-[*3..5]-(end) RETURN p
  5. Specify all path lengths: MATCH p=(start)-[*]-(end) RETURN p

About (1): Exact lengths are the most efficient if you know exactly how long the paths should be.

About (2): Upper bounds are great for getting the entire neighborhood within that length or shorter, though may contain more paths than you need as sub-paths will be matched for each longer path. Meaning that if (a)--(b)--(c) matches, then so will (a)--(b) and you'll get both paths in the result set.

About (3): Lower bounds with no upper bounds is probably never a great idea when end is not known. More at (5)

About (4): A lower bound plus upper bound range around a known node is interesting, like a donut of the outer neighborhood.

About (5): Remember the redundancy of (2)? Multiply that by finding all reachable nodes in the graph from the starting node. Phew, wow. Best to avoid unless end is known and you limit the results to 1 path, effectively asking "is there a path from start to end".

Through all this keep in mind the not-so-obvious fact that MATCH is finding paths, not graphs. On the receiving side, you need to stitch a graph back together from those paths. Neo4j Browser's graph visualization does that post-processing whenever a result set contains any graph elements (node, relationships or paths).

Best,
ABK

Hi @abk,

My bad, should've realized the length was the problem there. It works perfectly now. I can retrieve all the data now. Thank you so much for the advice as well. Really appreciate the help you've given me.