Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-05-2022 09:10 AM
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
Solved! Go to Solution.
07-06-2022 07:10 PM
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
07-05-2022 02:06 PM
What is your definition of ‘degree’
and what is the ‘root node’
07-06-2022 01:19 AM
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
07-06-2022 02:50 AM
What is the degree for nodes indirectly connected to the root that are more than one hop away?
07-06-2022 03:15 AM
here degree means the level
for instance
for example
rootNode -- [1st Degree] ---> node x ---- [2nd Degree ]---> node y
07-06-2022 03:24 AM
Got it, basically number of hops away from root. What is the max value of ‘n’ you want this for?
07-06-2022 03:30 AM - edited 07-06-2022 03:31 AM
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}
07-06-2022 03:57 PM - edited 07-06-2022 07:08 PM
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
07-06-2022 07:10 PM
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
07-07-2022 01:08 AM
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
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
07-07-2022 02:28 AM - edited 07-07-2022 02:34 AM
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
07-07-2022 07:50 AM
Thank you so much for helping me. and no need to say sorry😊
All the sessions of the conference are now available online