EagerResult doesn't return the segment information like relationship type

Hi,

I'm digging into processing the result returned by executing a query via neo4j driver. But I found the default result doesn't contain the information of relationship type (unlike the result when I use the interface of the Neo4j Desktop/Web that it has segments information). I'm testing the case that returns a path (can be multiple connected sub-graphs)

How can I solve this.

Can you post your code?


Here is the result:

paths do have the relationships. Maybe what you are viewing is the path representation as a string to show in your IDE. You should be able to get the relationships from the relationships method: relationships(path).

Instead of returning the path, you could return the nodes and relationships.

Return nodes(path) as nodes, relationships(path) as relationships

question: what is the point of the second match, the one from ‘e’ to ‘b’? You don’t use it in your results. Is it included because you need the path to exist ?

It's a condition on (e). I have 2 conditions, but I don't want to write like (a)-[r1]-(e)-[r2]-(b) where a =... and b = ... what if I have 3 condition on 3 relationship of e?

You can use the "exists" predicate instead. This avoids finding all the matching paths and returning rows for each combination of results. Plus, the predicate will stop once it is true, and stop looking for other paths.

match (a) where a.id='TranChien0051'
match (b) where b.id='DiaDiem00029'
match (a)-[:ten_goi]-(e:Event)
where exists((e)-[:dien_ra_o|thuoc*]-(b))
match (e)-[:subEvent|mo_ta]->(e1)
match path=(e1)-[:Causal|Result|SubEvent*0..1]->()
unwind nodes(path) as l
match result=(l)-[*0..1]-(:Entity)
return result

Hello I just check again this. I realize that the relationship is actually relationship of the last path, which are relations between Event and Entity, It does not contain relationship between Event - Event like Cause|Result|Sub or NextEvent in the first path of my query (line 5) . I want to get it too. Just like the visualization. But the return of query isn't comprehensive

What is it that you want? The graph you show has a backbone linking nodes (I assume Event nodes) with HAS_NEXT relationships. We did not capture any of this. Can you describe your data model and explain what you need extracted?

I don't see any relationship has type "HAS_NEXT". Okay let's analyze the query. This is what we have:

match path=(e1)-[:Causal|Result|SubEvent*0..1]->()
unwind nodes(path) as l
match result=(l)-[*0..1]-(:Entity)

Given path includes: A, B, C which are all 3 Event Type nodes

Then lets see howl result created:

match result=(l)-[*0..1]-(:Entity) means from 3 nodes A,B,C which are in the l variable (I have to emphasize: we don't keep the relationship between 3 nodes in variable l), we spread its relations by Entity Type nodes.

-> So the final result contains 3 Event Types nodes separately and its relations.

What I want is to keep the relation between 3 nodes which are NextEvent and keep the relation of Child Nodes of these 3 nodes (if any) which are Causal|Result|SubEvent
I mean I want to get the whole graph as in the picture.

I want to explain more about my approach:

  • For all query I want to define the frame path first which is all Event Nodes and theirs relations, that would be the main result. Then I full fill the information of the path by spread its Entity Role (l)-[*0..1]-(:Entity) I set the type Entity to constraint the spread path, only roles of the detected Event can be spread or else other irrelevant Event will be included too.

Oh I realize that i was wrong. As I test my query in the UI/UX and it's automatic show relation between my nodes.

I explain again my answer above. The problem is to get the nodes A, B, C I use this match:

(e)-[:subEvent|mo_ta]->(e1) means A, B, C play roles e1. And the relation between each other is NextEvent to express the order of timeline which one occur first. That is my data Ontology. So how to get the relation between them?

And why I don't spread NextEvent in path=(e1)-[:Causal|Result|SubEvent*0..1]->()

It like a depth level of spreading. And to avoid of including some irrelevant information. All SubEvent of a Parent Node must be connected to each other by NextEvent, but the child node itself might contains a lot of other Events. I call it depth-level 2. To catch those unknown depth. I use Causal|Result|SubEvent*0..1 ja, I said unknown is not correct here because I set limit 0..1 to speed up the query performance, and currently we only accept 2 level in our Ontology.

So all the relations between Event Node are missing in the result.

Okay, I figure out a way but it needs a post processing step because information might be duplicated, neo4j return list of records

match (e:Event)-[:ten_goi]-(a) where a.id  ="TranChien0051" 
match (e)-[:(dien_ra_o|thuoc)*]->(b) where b.id  = "DiaDiem00029"
match (e)-[:SubEvent|mo_ta]->(e1)-[r1:NextEvent]-()
match path=(e1)-[r2:(Causal|Result|SubEvent)*0..1]->()
unwind nodes(path) as l
match result = (l)-[*0..1]-(y:Entity)
return nodes(result) as nodes, labels(l) as labels, relationships(result) as roles,  r1, r2

I realize that if use unwind function, i still can access variable had declared before instead of with ,

I think you are getting duplicates because of your multiple sequential matches can create duplicates.

Your query has several matches that look for items that are not used, so I am wondering if a more direct approach can be found. Unfortunately, I still don't have a clear understanding of what you are looking for. You are specifying two nodes "a" and "b". You start at "a" in your first match, then extend the path to "b" in the second match. You don't use the results from the second match in your query, so what is the need for this? Are you trying to find the "e" nodes from the first query that have a path to "b"? If so, putting this in a "where" clause would be more efficient, as it will stop looking once a single path is found.

The rest of the matches confusing me as well. How does this related to the graph you provided. Is the backbone of the graph the nodes between "a" and "b"? What portion(s) of that graph are you trying to extract?

If you look closely, it for finding, given some information. Use for question answering problem. I might not be good at query but I don't think its hard to understand @@

just track from below and go up. the result need path the path need e1 and to find e1 I need information of a, b, and e these are all related to e1.

actually I query that query and the graph shown there is the result when use the UI/UX neo4j

The graph shown from querying in the browser may not represent your query results, as the browser by default shows all the relationships between each node displayed, even if the relationship is not part of your result.

I am happy if you got what you needed.

I'm reading about EXISTS statement and still confused as you mention "stop"

like how it can be dupplicated with this:

match (e:Event)-[:ten_goi]-(a) where a.id  ="TranChien0051" 
match (e)-[:(dien_ra_o|thuoc)*]->(b) where b.id  = "DiaDiem00029"

and how this can help?

match (a) where a.id='TranChien0051'
match (b) where b.id='DiaDiem00029'
match (a)-[:ten_goi]-(e:Event)
where exists((e)-[:dien_ra_o|thuoc*]-(b))

and what if I have another condition can I use and condition with exists statement?