0
votes

I have a Graph database with over 2 million nodes. I have an application which takes a social graph and does some inference on it. As one step of the algorithm, I have to get all possible combinations of a relationship [:friends] of two connected nodes. Currently, I have a query which looks like: match (a)-[:friend]-(c), (b)-[:friend]-(d) where id(a)={ida} and id(b)={idb} return distinct c as first, d as second

So, I already know the nodes a and b and I want to get all the possible pairs that can be made from friends of a and b. This is obviously a very slow operation. I was wondering if there is a more efficient way of getting the same result in neo4j. Perhaps adding indexes might help? Any ideas / clues are welcome!

Example Node a has friends : x, y Node b has friends : g, h, i`` Then the result should be: x,g x,h x,i y,g y,h y,i`

2

2 Answers

1
votes

If you are not already you should use labels to speed up your query, which might look like:

MATCH (p1:Person)-[:FRIEND]->(p3:Person),(p2:Person)-[:FRIEND]->(p4:Person)
WHERE ID(p1) = 6 AND ID(p2) = 7
RETURN p3 as first, p4 as second

Obviously that will rely on you having created your nodes with a :Person label.

How many friends does the average node have?

0
votes

I wouldn't use two patterns but just one and the IN operator.

MATCH (p:Person)-[:FRIEND]->(friend:Person) 
WHERE id(p) IN [1,2,3]
RETURN p, collect(friend) as friends

Then you have no cross product and you can also return the friends nicely as collection per person.