How to return nodes that have a relationship as a group along with path length

I have this Cypher query:

MATCH path = (l:Partner)-[:HAS_MT4]->(n:Mt4)-[:HAS_REF*]->(:Mt4)<-[:HAS_MT4]-(m:Partner)
WHERE m.partner_id IN [39001174]
  AND EXISTS(n.mt4_id)
  AND n.account_type <> 'demo'
RETURN m.partner_id AS partner_id, l.partner_id AS sub_partner, LENGTH(path) AS level

Node labels are Partner and Mt4 and the relationship types are HAS_MT4 and HAS_REF.

There are 3 partners (123456, 852963, 741852), and also a master partner 39001174.

123456 is under 39001174 and this is a direct relationship. 741852 is under 852963 who is under 39001174. So here we have two levels that should appear together.

If I run the query above for this example I will get the following result:

partner id = [39001174,39001174,39001174]
sub partner = [123456,852963,741852]`
level = [1,1,2]

But what I want to get is the following:

partner id = [39001174,39001174]
sub partner = [{123456:1},{852963:1,741852:2}]

Could you please help me, as I am a beginner in neo4j?

I am sorry, I don’t understand how your result is lists instead of rows of data.

What defines a partner and sub partner nodes?

Hello. They are not lists. They are rows
partner_id sub_partner level
39001174 123456 1
39001174 852963 1
39001174 741852 2

And I want to return
partner_id sub_partner
39001174 {123456:1}
39001174 {852963:1,741852:2}

Subpartner is an alias that is derived from l:Partner

123456 -> 39001174
741852 -> 852963->39001174

So 39001174 has two networks. One that includes only 123456 and the other that includes 741852 and 852963 with 852963 being the first node directly connected (level 1) and 741852 in level 2

I am confused how you get lengths of 1 and 2, when there are three required relationships in your match query.

From what I now understand, the 'm' node is the master partner and the 'l' node is the related partner. Where does the hierarchy come in to play?

Do you have a script that I can use to generate some test data?

You are right. the level actually would be 3 for 123456 and 3 and 4 for 852963 and 741852 respectively. But still the concept is the same.

Master partner is 39001174 (m node)
one partner that is referred to him is 123456 (l node)

Then we have partner 741852 who is referred to 852963 who is referred to 39001174
So two different networks, one including one partner and the other two, both referring to 39001174
Sorry no script

Your query returns only the 'm' and 'l' node data, which I assume 'm' is the master partner and 'l' is the directly related partner. How do you identify the partner at the second level.


Maybe this image can help?


If I print the relationship it is more clear. So these nodes are coming together.
Node id 1376727 is our master partner, node id 1385745 is directly connected to master and node id 1385749 is connected to node id 1385745 . So I know that the second dictionary is level 1 and the first dictionary level 2

If this is getting too complex could you please at least help me achieve the following?
Given the last screenshot with the relationship, is it possible to create a list with the format below?
[["start","end"],["start","end"]....]
In this example we will have
39001174 [[1385749,1385745],[1385745,1376727]]

Using variables in a multiply-hop pattern is deprecated, such as 'r' in '[r:HAS_REF*]'. I did it the new way. See if this works:

MATCH path = (l:Partner)-[:HAS_MT4]->(n:Mt4)-[:HAS_REF*]->(:Mt4)<-[:HAS_MT4]-(m:Partner)
WHERE m.partner_id IN [39001174]
  AND EXISTS(n.mt4_id)
  AND n.account_type <> 'demo'
RETURN m.partner_id AS partner_id, LENGTH(path) AS level, [i in relationships(path) where type(i)='HAS_REF' | [id(startNode(i)), id(endNode(i))]]

I don't think it is too complicated yet. In your figure, how do I know that the red nodes are associated with each other and the yellows with each other? Are the two red nodes and the master partner all along the same path and the yellow ones along the same path? In effect there could be 'Partner' nodes within the multiple-hop segment of your pattern: '[:HAS_REF*]'?

This one with the list works as expected! thank you very much!

As for your question, yes the partners in the different circles form one network. I double checked it with the relationship. They are in the same list.

I am not sure I understood this part
" In effect there could be 'Partner' nodes within the multiple-hop segment of your pattern: '[:HAS_REF*]'?"

HAS_REF connects the Mt4 nodes. Partners connect to each other with mt4 accounts. HAS_REF is a relationship only between Mt4 nodes

Oh, so a network with two levels of partners would be like this:

(l:Partner)-[:HAS_MT4]->(:Mt4)-[:HAS_REF*]->(:Mt4)<-[:HAS_MT4]-(m1:Partner)-[:HAS_MT4]->(:Mt4)-[:HAS_REF*]->(:Mt4)<-[:HAS_MT4]-(m2:Partner)

Here we have the master node 'l' and two connected partner nodes 'm1' and 'm2'?

If this is true, do you want all the partners along the path and their depths? What depth of a network?

Does this accurately find the entire network:

match path=(l:Partner)-[:HAS_MT4]->(n:Mt4)-[:HAS_MT4|HAS_REF*]-(m:Partner)
WHERE m.partner_id IN [39001174]
  AND EXISTS(n.mt4_id)
  AND n.account_type <> 'demo'
and not exists((:Mt4)<-[:HAS_MT4]-(l:Partner)--(n))
return *

Sorry I got a bit confused

MATCH path = (l:Partner)-[:HAS_MT4]->(n:Mt4)-[:HAS_REF*]->(:Mt4)<-[:HAS_MT4]-(m:Partner)
WHERE m.partner_id IN [39001174]
AND EXISTS(n.mt4_id)
  AND n.account_type <> 'demo'
return l,m,n

In my initial query, m node is the master Partner. l nodes are all the partners that are linked to m, in this case 39001174. So this query will return the entire network. Hope this helps

Those are mt4 nodes. I thought you want to know the relationship between a master partner (node 'm', such as '39001174') and the other partners nodes 'l' (such as partners '741852' and '852963'). You state that '741852' and '852963' are related. How are they related in the graph, so I can get them and derive their 'level'?

Does this help? These are the same nodes from the previous screenshot. All of them are in the same network as we saw from the relationship list that you provided earlier. The relationship between the partners is derived from the mt4 nodes.

so in this example if we end up with [[41461061:6],[41454701:5],[41221677:4]]
or [[4569840:6],[4569730:5],[4563800:4]] is exactly the same

Is it correct to say, that you have networks that start on a Partner node (which you give for the query) and terminate on another Partner node. The number of terminating Partner nodes determines the number of networks. Within a network path thee can be various number of mt4 nodes.

What additional information from the paths do you need? From you first posts, you had two partner nodes related but at different levels. How is the relation determine between to sub_partners?

For our case lets assume that we want only the paths that terminate to 39001174. Therefore where statement WHERE m.partner_id IN [39001174] will be like this. All paths will terminate to this master.

For the other question I am not sure I understand, sorry. I just need the partner id and the level and group whoever is in the same path

All nodes along the same path, which includes mt4 and partner nodes? For each node, you want its id and level, and you want the levels grouped?

For partners (or MT4ids, it doesn't really matter) that are in the same path I want the partner id (or node id, or mt4 id, again it doesn't matter) and level as a group. Kind of like the list you helped me create out of the relationship. But now it will be something like this [[partner_id(or mt4id),level],....]