我有一个neo4j社交网络数据库,并有一个用例来查看一堆用户ID,并检查图中有多少用户ID。用户看起来像这样:
@NodeEntity
public class User {
@GraphId
Long nodeId;
@Indexed(indexName = "uid",unique = true)
Long uid;
}
my check would look somrthing like this :
for(Long uid : allUserIds){
User friend = userRepo.findByPropertyValue("uid", uid);
if(friend!=null){
//Create a relationship
}else{
//continue
}
}
有没有办法可以为每一个userId摆脱findByPropertyValue?有没有更快的方法可以让我在一个请求中给出一堆uid所有现有用户?
Thanks..
答案 0 :(得分:2)
您可以尝试使用Cypher查询:
Map<String, Object> params = new HashMap<String, Object>();
String query = "start user=node:__types__(className=\"<package>.User\") where ID(user)>=0 and ID(user) in {uids} return user"; // substitute <package> with the full package name
params.put("uids", allUserIds); // allUserIds should be a Collection<Long>
Collection<User> users = neo4jOperations.query(query.toString(), params).to(User.class).as(Collection.class);
for (User user: users) {
...
}
答案 1 :(得分:1)
你已经做对了。
还有findByQuery afaik,它允许你传入一个lucene查询"uid: value1 value2 value3"