Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-21-2021 12:46 AM
Hi. I posted some days ago a question about Cypher query on relationships. I repost the question here:
"Suppose to have the graph below.
I want to return all the nodes connected to the node A
and the maximum of the values on the edges between two consecutive nodes(which is an attribute of the relation). I show an example in the picture below."
The solution was :
MATCH path=(a {id:"A"})<-[:HAS*]-(b)
RETURN a,b, max([r in relationships(path) | r.value])
Now. Suppose that we have a second property/attribute on the relationship (which is called secondV in my example). How can I return also the second attribute in my query?
I tried
MATCH path=(a {id:"A"})<-[:HAS*]-(b)
RETURN a,b, max([r in relationships(path) | r.value]) , [r in relationships(path) | r.secondV]
But it returns to me all the possible couples of secondV with the maximum value I got.
The result should be as follow:
Solved! Go to Solution.
10-25-2021 01:27 AM
No. This doesn't work. I think I find the solution with this:
MATCH path=(startNode:Product {id: 'A'})-[:HAS*]->(endNode)
with startNode,endNode, max([r in relationships(path) | value]) as valueList
match path = (startNode) - [:HAS*] - >(endNode)
with startNode,endNode, valueList,[r in relationships(path) where value in valueList | secondV ] as secondValueList,path
where size(valueList) = size(secondValueList)
return distinct startNode,endNode,valueList,secondValueList, [x IN nodes(path) | x.id] as nodes
I'm not sure if it works in every case
10-21-2021 05:44 AM
@AjejeBrazorf
If I understand correctly, in this way it should work:
MATCH path=(a {id:"A"})<-[r:HAS]-(b)
with max(r.value) as max, a, b
call {with max, a, b match (a)<-[rel:HAS]-(b) where rel.value = max return rel limit 1}
return a, b, rel.value, rel.secondV
that is, I search for a max value,
then I call a subquery to retrieve the relation (for each a,b couple),
and finally I return value
, secondV
, a
and b
10-21-2021 06:10 AM
This works for path of length one. I need to call the procedure for longer paths
10-21-2021 08:15 AM
Oh, I missed that, the drawing confused me :X
Then, can you try with this? (Basically the same logic as above, I added [0]
to max
in 2nd row because is a list)
MATCH path=(a {id:"A"})<-[:HAS*]-(b)
with max([r in relationships(path) | r.value])[0] as max, a, b
call {
with max, a, b match path=(a)<-[:HAS*]-(b) unwind relationships(path) as rel with rel, max where rel.value = max return rel limit 1
}
return a, b, rel.value, rel.secondV
10-22-2021 12:29 AM
I tried this solution, But it returns only the first value of the list:
value: 8 / secondV : 4
It doesn't return the other the relationships.
10-22-2021 08:58 AM
Hi @AjejeBrazorf !
I'll suggest a solution with apoc for the sake of sanity
MATCH (a {id:"a"})
CALL apoc.path.expandConfig(a, {
relationshipFilter: "HAS",
minLevel: 1,
uniqueness: 'NODE_PATH'
})
yield path
with relationships(path) as rels, nodes(path) as no
unwind rels as r
RETURN head(no).id as begin, last(no).id as end, apoc.agg.maxItems(r, r.value).items
Bennu
10-25-2021 01:27 AM
No. This doesn't work. I think I find the solution with this:
MATCH path=(startNode:Product {id: 'A'})-[:HAS*]->(endNode)
with startNode,endNode, max([r in relationships(path) | value]) as valueList
match path = (startNode) - [:HAS*] - >(endNode)
with startNode,endNode, valueList,[r in relationships(path) where value in valueList | secondV ] as secondValueList,path
where size(valueList) = size(secondValueList)
return distinct startNode,endNode,valueList,secondValueList, [x IN nodes(path) | x.id] as nodes
I'm not sure if it works in every case
All the sessions of the conference are now available online