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.

Nth order relationships: sort by property in first order, followed by same property in second order

Hi, I have a graph database of people (nodes) and one type of relationship between them. The relationship has a "strength" property in the form of an integer.

What I am trying to do is:

  • get a specific person
  • find the top 10 (strongest) contacts of this one node
  • for each of the 10 nodes returned, return the 2 strongest contacts of that node.

I would want around 30 nodes to be returned... 10 first order nodes and 2 second order nodes for each of those

I have tried many different queries, typically like this:

MATCH (a:Person {EmailAddress: 'test@test.com'})-[rel]-(b:Person)-[rel2]-(c:Person)
RETURN a, rel, b, rel2, c order by rel.Strength desc, rel2.Strength desc limit 30

My problem is that I am getting one first order node, and 29 second order nodes, "eating up" all my limit of 30.

Is there a way to force a limit on each level? In other words, specify multiple "limit" values?

Thanks

1 ACCEPTED SOLUTION

Try this. This will give you a list of 20 rows, consisting of the product of the 10 strongest nodes and their two strongest nodes each. I don't understand the 30 limit.

match(a:Person{EmailAddress: 'test@test.com'})-[rel]-(b:Person)
with a, rel.strength as strength_ab, b
order by strength_ab desc
limit 10
with a, strength_ab, b
call{
with b
match(b)-[rel]-(c:Person)
with b, rel.strength as strength_bc, c
order by strength_bc desc
limit 2
return strength_bc, c
}
return a, strength_ab, b, strength_bc, c

View solution in original post

3 REPLIES 3

Try this. This will give you a list of 20 rows, consisting of the product of the 10 strongest nodes and their two strongest nodes each. I don't understand the 30 limit.

match(a:Person{EmailAddress: 'test@test.com'})-[rel]-(b:Person)
with a, rel.strength as strength_ab, b
order by strength_ab desc
limit 10
with a, strength_ab, b
call{
with b
match(b)-[rel]-(c:Person)
with b, rel.strength as strength_bc, c
order by strength_bc desc
limit 2
return strength_bc, c
}
return a, strength_ab, b, strength_bc, c

NICE! Worked almost immediately and at first glance it seems to have done what I wanted... Thank you for this, I would not have figured it out easily alone, and your reply opened up a whole new area of things to experiment with 🙂

Thanks again

EXCELLENT. It was fun.