sharath
sharath

Reputation: 3626

Spring Data Neo4j : find all nodes with a list of propertyvalues

I have a neo4j social network db and have a usecase to look through a bunch of user ids and check how many user ids are present in the graph. The User looks like this :

@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
        }
    }

Is there a way I can get rid of the findByPropertyValue for every single userId ? Is there a faster way thru which I can get all existing Users given a bunch of uids in one request ?

Thanks..

Upvotes: 0

Views: 757

Answers (2)

remigio
remigio

Reputation: 4211

You could try with a Cypher query:

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) {
    ...
}

Upvotes: 2

Michael Hunger
Michael Hunger

Reputation: 41706

You're already doing it right.

There is also findByQuery afaik, that allows you to pass in a lucene query which would be "uid: value1 value2 value3"

Upvotes: 1

Related Questions