Skip to content

Instantly share code, notes, and snippets.

@javrasya
Last active January 1, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javrasya/8123755 to your computer and use it in GitHub Desktop.
Save javrasya/8123755 to your computer and use it in GitHub Desktop.
//maxRelationLevel is max relation level that you likely use. In your situation, this is 2. Cause, your levels are 1,2,null(ALL)
public String getQuery(String userId, List<String> userIds, Integer relationLevel, int maxRelationLevel) {
String startQuery = "start user1=node:node_auto_index(userId = %s), user2=node('name:(%s)')".format(userId, StringUtils.join(userIds,","));
String whereQuery = ""
List<String> whereQueryList = new ArrayList<String>();
if (null != relationLevel) {
for (i = 1; i <= relationLevel,i++){
whereQueryList.add("(user1-[knows*%s]->(user2) AND user2.preferedLevel>=%s)".format(i, i);
}
whereQuery = StringUtils.join(whereQueryList, " and ");
} else {
//relation level must be null when status ALL. If the status is ALL, there will be no preferedLevel field for the record in Neo4J DB.
whereQueryList.add("not has(user2.preferedLevel)")
for (i = maxRelationLevel, i >= 0, i--) {
whereQueryList.add("(user2.preferedLevel=%s and user2-[knows*%s]->(user1))".format(i, i))
}
whereQuery = StringUtils.join(whereQueryList, " or ");
}
return "%s %s".format(startQuery, whereQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment