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 add a degree property to a node for nth degree relationships

Hi there,

I'm looking for help. I'm trying to find degree of node against the root node. 

// here n is a variable like 1,2,3,4 .... nth
match p=(n:Config)-[*..(n)]-(b) where n.id= "Conf001" 
        unwind nodes(p) as nodes  unwind relationships(p) as re
        with collect(DISTINCT{id: nodes.id, label: COALESCE(nodes.name, nodes.title), type:labels(nodes)[0]}) as nl,
        collect(DISTINCT{id:ID(re),label: TYPE(re), source: properties(startNode(re)).id, target: properties(endNode(re)).id}) as rl
        return {nl: nl, rl: rl}

 this query return me all nodes and all relationships here is the output 


{
    "nl": [
        {
            "id": "Conf001",
            "label": "node 1",
            "type": "Config"
        },
        {
            "id": "M00006",
            "label": "node 2",
            "type": "Model"
        },
        {
            "id": "M00004",
            "label": "node 3",
            "type": "Model"
        }
    ],
    "rl": [
        {
            "id": 40,
            "label": "MARKETED_UNDER",
            "source": "Conf001",
            "target": "M00006"
        },
        {
            "id": 38,
            "label": "MARKETED_UNDER",
            "source": "Conf001",
            "target": "M00004"
        }
    ]
}

 What I also want to get degree of the node like this

 
 "nl": [
        {
            "id": "Conf001",
            "label": "node 1",
            "type": "Config",
            "degree": 0
        },
        {
            "id": "M00006",
            "label": "node 2",
            "type": "Model",
            "degree": 1
        },
        {
            "id": "M00004",
            "label": "node 3",
            "type": "Model",
            "degree": 1
        }
    ]

Can somebody guide me 

Thank you so much

1 ACCEPTED SOLUTION

I refactored the query using a slightly different approach to make it more compact.  Its a matter of preference. They both gave me the same results:

 

match p=(r:Test{id:0})-[:REL]->(:Test1)-[:REL]->(:Test2)
with nodes(p) as nodes, relationships(p) as relationships
with nodes, range(0, size(nodes)-1) as indexes, relationships
with [x in relationships | {relId: id(x), id: x.id, type: type(x)}] as relList,
     [x in indexes | {nodeId: id(nodes[x]), id: nodes[x].id, label: labels(nodes[x])[0], degree: x}] as nodeList
unwind nodeList as nodeMap
unwind relList as relMap
return collect(distinct nodeMap) as nls, collect(distinct relMap) as rl

 

View solution in original post

11 REPLIES 11

What is your definition of ‘degree’

and what is the ‘root node’ 

Thank you so much for your reply 


 what is the ‘root node’ 

here root node is "Conf001"

match p=(n:Config)-[*..(n)]-(b) where n.id= "Conf001"

What is your definition of ‘degree’
The degree is the relation between the root node and nodes which is directly connected to this node 

for instance this is the root node which is specify in the condition

{
            "id": "Conf001",
            "label": "node 1",
            "type": "Config"
        },

and these nodes have direct relationships to this ("Conf001") node. 

 {
            "id": "M00006",
            "label": "node 2",
            "type": "Model",
            "degree": 1
        },
        {
            "id": "M00004",
            "label": "node 3",
            "type": "Model",
            "degree": 1
        }

 Thank you so much 

What is the degree for nodes indirectly connected to the root that are more than one hop away?

here degree means the level 

for instance

Screenshot 2022-07-06 154433.png

for example 
rootNode -- [1st Degree] ---> node x ---- [2nd Degree ]---> node y

Got it, basically number of hops away from root. What is the max value of ‘n’ you want this for? 

here n is variable and max value is 10 

 

I'm using this query 

match p=(n:Config)-[*..(n)]-(b) where n.id= "Conf001" 
        unwind nodes(p) as nodes  unwind relationships(p) as re
        with collect(DISTINCT{id: nodes.id, label: COALESCE(nodes.name, nodes.title), type:labels(nodes)[0]}) as nl,
        collect(DISTINCT{id:ID(re),label: TYPE(re), source: properties(startNode(re)).id, target: properties(endNode(re)).id}) as rl
        return {nl: nl, rl: rl}

This seems work.  I derived the node's level from its position in the path away from the root node. You can adapt it to your use case and pretty it up. 

 

match p=(r:Test{id:0})-[:REL]->(:Test1)-[:REL]->(:Test2)
with nodes(p) as nodes, relationships(p) as relationships
with nodes, range(0, size(nodes)-1) as indexes, [x in relationships | {relId: id(x), id: x.id, type: type(x)}] as rels
unwind indexes as index
with nodes[index] as node, index, rels
with {nodeId: id(node), id: node.id, label: labels(node)[0], degree: index} as nodeMap, rels
unwind rels as rel
return collect(distinct nodeMap) as nls, collect(distinct rel) as rl

 

 

I refactored the query using a slightly different approach to make it more compact.  Its a matter of preference. They both gave me the same results:

 

match p=(r:Test{id:0})-[:REL]->(:Test1)-[:REL]->(:Test2)
with nodes(p) as nodes, relationships(p) as relationships
with nodes, range(0, size(nodes)-1) as indexes, relationships
with [x in relationships | {relId: id(x), id: x.id, type: type(x)}] as relList,
     [x in indexes | {nodeId: id(nodes[x]), id: nodes[x].id, label: labels(nodes[x])[0], degree: x}] as nodeList
unwind nodeList as nodeMap
unwind relList as relMap
return collect(distinct nodeMap) as nls, collect(distinct relMap) as rl

 

Thank you so much for answering. I'm trying to understand the query. 

p=(r:Test{id:0})-[:REL]->(:Test1)-[:REL]->(:Test2)

in my case, I have only  two pieces of information 

  1.  Config (Label name)
  2. properties of the label id= "Conf001" (property)

I have no information about the relationship node label in advance like in query (:Test1)-[:REL]->(:Test2)

and "Test" has many relationships suppose 10 relationships 

in my query, I didn't specify the label  

(n:Config)-[*..(n)]-(b)

Do you have any advice for me 
Thank you so much again for helping me 

Sorry, I should have explained. Replace line 1 with your match pattern. Just ensure it is still set to ‘p’. That was what I used to test it against some test data I created. 
Also, change the map projections to include the properties you want. Lines 4 and 5 in the second version of query

Thank you so much for helping me. and no need to say sorry😊