Getting relationships from Golang Driver

When I execute the following query in the Neo4j Browser, it produces a completely connected visual graph. But when I switch to table view it only contains records corresponding to the nodes. i.e. no info about the relationships.

MATCH (n) RETURN n

The table view corresponds to the result I get when I execute the same query using the Golang Driver.

Is the browser executing a different query or modifying mine to get the results to populate the graph view?

How can I modify my query from Golang to get the relationships, so I can construct the graph?

The browser shows all relationships between displayed nodes. You can disable the feature. Click on the settings icon (gear) in the browser window. Scroll down to the bottom and uncheck the box that refers to showing connections

What query are you using. The one you quoted deletes everything.

Oops I pasted the wrong query, I've corrected it. I didn't want to complicate the question with my exact query. So I was just using the example of retrieving all the Nodes.

But here is the query I'm executing.

MATCH (e:Entity {id: '#root'})
WITH e, [(e)<-[:CHILD_OF|ATTACHED_TO*]-(x) | x] as ancestors,  [(x)<-[:CHILD_OF|ATTACHED_TO*]-(e) | x] as descendants
RETURN e, ancestors, descendants

I want to be able to construct the graph resulting from this query in my Go code. And I'm only getting back nodes, and no relationship info between those nodes.

Is there a simple way for me to do the same thing the browser is doing, by indicating to the query that I want connections returned also?

Maybe this will work.

RETURN *

I didn't know how it would interact with the variables in the WITH part of the query.

The following query doesn't return the relationships either.

MATCH (e:Entity {id: '#root'})
WITH e, [(e)<-[:CHILD_OF|ATTACHED_TO*]-(x) | x] as ancestors,  [(x)<-[:CHILD_OF|ATTACHED_TO*]-(e) | x] as descendants
RETURN *

Would you expect it to?

No, the query does not reference and return the relationships; the query returns only nodes. The path patterns are variable length. Your query only returns the terminating node along each path. I will assume you want everything so you can reconstruct the graph. There are a lots of ways to output the data; I will just pick one. We can refine it to meet your specific needs if you can define what you are looking for.

The following query finds the longest path along each ascendant and descendant path. It also returns only the relationships along each path. I did this because each relationship contains its startNode and endNode, as well as its relationship type and properties. You can obtain the start node of a relationship 'r' with method startNode(r) and the end node with endNode(r). Relationship properties are obtained with properties(r) and its type with type(r).

MATCH (e:Entity {id: '#root'})
optional match ancester_paths = (e)<-[:CHILD_OF|ATTACHED_TO*]-(x1) 
where not exists ((x1)<-[:CHILD_OF|ATTACHED_TO]-())
optional match descendant_paths = (x2)<-[:CHILD_OF|ATTACHED_TO*]-(e)
where not exists (()<-[:CHILD_OF|ATTACHED_TO*]-(x2))
RETURN e, collect(relationships(ancester_paths)) as ancestor_relationships, collect(relationships(descendant_paths)) as descendant_relationships

Is your graph a star topology, or will the ascending and descending paths have common nodes and edges? If so, maybe one of the apoc path procedures is more efficient. @bennu.neo or @ameyasoft, do you have any suggestions?

Hi all,

Maybe something like this could help:

MATCH (n:Entity {id: '#root'})
with n
CALL apoc.path.subgraphAll(n, {
    relationshipFilter : 'CHILD_OF>|ATTACHED_TO>',
    optional: true
}) yield nodes, relationships
with n, nodes as ascNodes, relationships as ascRel
CALL apoc.path.subgraphAll(n, {
    relationshipFilter : '<CHILD_OF|<ATTACHED_TO',
    optional: true
}) yield nodes, relationships
with n, nodes as descNodes, relationships as descRel , ascNodes, ascRel
return *

Bennu

1 Like

Thanks! I'll give these a try.

My graph is a tree. Given a node in the tree, I want to find the subgraph that includes its descendants, as defined by the CHILD_OF and ATTACHED_TO relationships. and its ancestors as defined by the same relationships.

Considering it’s a tree, I think the apoc approach will be better. The backend algorithm is traversing the tree and accumulating the results. It’s should traverse each edge once.

The cypher approach is looking for individual paths. The resultant paths will have lots of duplicate nodes and relationships to address.

Excellent @bennu.neo's solution worked! Thank you for the help!

2 Likes

You are welcome @mburbidg1 !

Now you are set for a weekend of 2 Champions Finals, F1 and more!