Neo4J查询可带回用户兴趣和其他已知用户

时间:2018-11-12 18:07:51

标签: neo4j cypher spring-data-neo4j

我有一个现有的Neo4查询,以带回用户兴趣,以及这些兴趣是否相关。查询是:

MATCH (u:User)-[r1:INTERESTED_IN]-(i1:Interest)
WHERE u.emailAddress = 'bob@mail.com'
OPTIONAL MATCH (u)-[r2:INTERESTED_IN]-(i2:Interest)
OPTIONAL MATCH (i1)-[r3:RELATED_TO]-(i2)
RETURN u, r1, i1, r2, i2, r3

这将映射为Spring Data Neo4J存储库方法,如下所示:

User findInterestsByEmailAddressIgnoreCase(@Param("emailAddress") String emailAddress);

我想扩展图形,以便用户可以认识其他用户(例如bob认识fred和tom以及fred和tom彼此也知道)。如何修改相同的查询以同时恢复这些关系?

我尝试为认识另一个用户的用户添加可选匹配,如下所示:

MATCH (u:User)-[r1:INTERESTED_IN]-(i1:Interest)
WHERE u.emailAddress = 'bob@mail.com'
OPTIONAL MATCH (u)-[r2:INTERESTED_IN]-(i2:Interest)
OPTIONAL MATCH (i1)-[r3:RELATED_TO]-(i2)
OPTIONAL MATCH (u:User)-[r5:KNOWS]-(u2:User)
RETURN u, r1, i1, r2, i2, r3, r5, u2

这似乎可以运行并带回neo4j浏览器中的图形,但是Spring Data似乎因错误而失败

  

org.springframework.dao.IncorrectResultSizeDataAccessException:   结果大小不正确:最多为1           在org.springframework.data.neo4j.repository.query.GraphQueryExecution $ SingleEntityExecution.execute(GraphQueryExecution.java:83)

我需要在查询中更改的任何指针吗?

更新

  1. 用户类别

    @NodeEntity
    public class User {
    
    @Id
    @GeneratedValue
    private Long id;
    
    public Long getId() {
        return id;
    }
    
    @Index(unique = true)
    private String emailAddress;
    
    private String name;
    
    @Relationship(type = "INTERESTED_IN", direction = Relationship.OUTGOING)
    private Set<UserInterest> interests = new HashSet<>();
    
    public Set<UserInterest> getInterests() {
        return interests;
    }
    
    public void setInterests(Set<UserInterest> interests) {
        this.interests = interests;
    }
    
    @Relationship(type = "KNOWS", direction = Relationship.UNDIRECTED)
    private Set<RelatedUser> relatedUsers = new HashSet<>();
    
    public Set<RelatedUser> getRelatedUsers() {
        return relatedUsers;
    }
    
    public void setRelatedUsers(Set<RelatedUser> relatedUsers) {
        this.relatedUsers = relatedUsers;
    }
    
    ... additional getters/setters
    
    }
    

1 个答案:

答案 0 :(得分:0)

根据Spring-data-neo4j docs,正确的方法签名是

 BookAdapter adapter = new BookAdapter(getActivity(), R.layout.list_item, Library.getInstance().getBible(bible).getBook(pos).getChapters());
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);