2
votes

I have a Cypher query which I'd like to expand to be summed up over a list of matching nodes.

My query looks like this:

 MATCH (u:User {name: {input} })-[r:USES]-(t) RETURN SUM(t.weight)

This matches one User node, and I'd like to adjust it to match a list of User nodes and then perform the aggregation.

My current implementation just calls the query in a loop and performs the aggregation outside of Cypher. This results in slightly inaccurate results and a lot of API calls.

Is there a way to evaluate the Cypher query against a list of elements (strings in my case)?

I'm using Neo4j 2.1 or 2.2.

Cheers

1

1 Answers

2
votes

You can use the IN operator and pass in an array of usernames:

MATCH (u:User)-[r:USES]-(t) 
WHERE u.name in ['John','Jim','Jack']
RETURN u.name, SUM(t.weight)

Instead of the array here you can use an parameter holding and array value as well:

MATCH (u:User)-[r:USES]-(t) 
WHERE u.name in {userNames}
RETURN u.name, SUM(t.weight)

userNames = ['John','Jim','Jack']