Reputation: 377
Let´s say we have a graph with nodes (n) and edges [e]. The edges all have properties, at least one of them numerical called 'weight'.
Now I want to know what the 'most tightly connected' neighboring (sibling / one-hop distance) nodes are for each respective node. As basis, I want to rank the neighboring nodes based on the weight property descending.
What would the query be for this in Neo4j? (in SQL, we could use a function like RANK())
e.g. let´s say my node has 4 neighboring nodes with weights (3,1,8,5). Then the query should return as rows a) the id of the node b) the weight of the node c) the overall rank of the node, so here in this case (3,4,1,2).
Upvotes: 0
Views: 178
Reputation: 425
you can use ORDER BY
to sort the results.
MATCH (n:NodeLabel {nodeid:YourNode})-[rel:RelationshipLabel]-(m:NodeLabel)
RETURN id(m) as NodeId, rel.weight as Weight
ORDER BY Weight DESC
Not sure about how to return rank.
Upvotes: 0
Reputation: 16033
Your example is not very clear to me, but if I understand correctly you want something like:
MATCH (n:Person)-[r]-(m)
WITH n, r.weight as w, m
ORDER BY w DESC
WITH collect ({name: m.name, w: w}) as ms, n
RETURN n, ms
Which for this sample data:
MERGE (b:Person {name: "Bob" })
MERGE (c:Person {name: "Charles" })
MERGE (a:Person {name: "Anna" })
MERGE (d:Person {name: "Kira" })
MERGE (e:Person {name: "Dan" })
MERGE (f:Person {name: "El" })
MERGE (a)-[:LIKES{weight: 7}]-(b)
MERGE (a)-[:LIKES{weight: 2}]-(c)
MERGE (a)-[:LIKES{weight: 1}]-(d)
MERGE (e)-[:LIKES{weight: 8}]-(a)
MERGE (e)-[:LIKES{weight: 3}]-(b)
MERGE (f)-[:LIKES{weight: 2}]-(a)
MERGE (d)-[:LIKES{weight: 5}]-(b)
Returns:
╒══════════════════╤═══════════════════════════════════════════════════╕
│"n" │"ms" │
╞══════════════════╪═══════════════════════════════════════════════════╡
│{"name":"Anna"} │[{"w":8,"name":"Dan"},{"w":7,"name":"Bob"},{"w":2,"│
│ │name":"Charles"},{"w":2,"name":"El"},{"w":1,"name":│
│ │"Kira"}] │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Dan"} │[{"w":8,"name":"Anna"},{"w":3,"name":"Bob"}] │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Bob"} │[{"w":7,"name":"Anna"},{"w":5,"name":"Kira"},{"w":3│
│ │,"name":"Dan"}] │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Kira"} │[{"w":5,"name":"Bob"},{"w":1,"name":"Anna"}] │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Charles"}│[{"w":2,"name":"Anna"}] │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"El"} │[{"w":2,"name":"Anna"}] │
└──────────────────┴───────────────────────────────────────────────────┘
Upvotes: 1