Mark,
Sorry for the delay. I have now convinced myself this must be some sort of user error (that took a while, I was getting similar problem to what you were reporting), with this test:
Code:
@Test
public void shouldRecommendFriends() throws Exception {
User a = users.save(new User("A"));
User b = users.save(new User("B"));
User c = users.save(new User("C"));
User d = users.save(new User("D"));
User e = users.save(new User("E"));
User f = users.save(new User("F"));
User g = users.save(new User("G"));
a.add(c);
b.add(c, d);
c.add(d, e);
d.add(f);
users.save(a);
users.save(b);
users.save(c);
users.save(d);
users.save(e);
users.save(f);
users.save(g);
assertFriendSuggestions(a, b, d, e);
assertFriendSuggestions(b, a, e, f);
assertFriendSuggestions(c, f);
assertFriendSuggestions(d, a, e);
assertFriendSuggestions(e, a, b, d);
assertFriendSuggestions(f, b, c);
}
private void assertFriendSuggestions(User user, User... expectedFriendSuggestions) {
List<User> actualFriendSuggestions = users.fofs(user.id, new PageRequest(0, 20)).getContent();
assertThat(actualFriendSuggestions.size(), is(equalTo(expectedFriendSuggestions.length)));
for (int i = 0; i < expectedFriendSuggestions.length; i++) {
assertThat(actualFriendSuggestions.get(i).name, is(equalTo(expectedFriendSuggestions[i].name)));
}
}
@NodeEntity
public class User {
@GraphId
Long id;
String name;
@RelatedTo(type = "FRIEND")
private Set<User> friends = new HashSet<User>();
public User() {
}
public User(String name) {
this.name = name;
}
public void add(User... users) {
friends.addAll(asList(users));
}
}
public interface UserRepositoryExtension extends GraphRepository<User> {
@Query("start user=node({0}) match user-[:FRIEND*2..2]-friendsOfFriends, user-[r?:FRIEND]-friendsOfFriends where r IS NULL and user <> friendsOfFriends return distinct friendsOfFriends order by friendsOfFriends.name asc")
Page<User> fofs(long id, Pageable pageable);
}
So, the query is the same as you had, in essence. I've added distinct results and used multi-step relationship match. I can't tell you what the problem was, but this version works. Also, it works with both unidirectional and bidirectional relationships.
Hope this helps!
Lasse