Search code examples
neo4junioncypherneo4jclient

Neo4j 1.9: alternative to Neo4j 2.0's UNION to combine result sets?


I'm trying to write a Cypher query for this social network type of application that I'm working on. Just think of a Facebook type of application where you have users who follow other users and the users can write posts and like/share them.

Let's say user Alice follows a couple of people (the subjects).

What the query should return is three things:

  1. the posts created by Alice's subjects
  2. the posts liked by Alice's subjects, with a number of likes >= 5
  3. the posts shared by Alice's subjects, with a number of shares >= 5

I'm able to write the three queries separately:

start alice=node(35500)
match (alice)-[:FOLLOWS]->()-[:SHARES]->(post)
where not((post)-[:ORIGINAL]->())
return post;

start alice=node(35500)
match (alice)-[:FOLLOWS]->()-[:LIKES]->(post)
with post, count(post) as likes
where likes >= 5
return post;

start alice=node(35500)
match (alice)-[:FOLLOWS]->()-[:SHARES]->(post)-[repost:ORIGINAL]->()
with post, count(repost) as reposts
where reposts >= 5
return post;

(a repost has an :ORIGINAL relationship to its original post)

At the moment I'm doing the three queries separately, combining, sorting and paging the results in the client. What I really want is a single query, where I can page the results with SKIP and LIMIT.

As far as I can tell, UNION is not supported in Neo4j 1.9, which is the (stable) version I'm using. Upgrading to an unstable Neo4j 2.0 is not really an option.

Is there a way to do this without Neo4j's UNION?

Thanks!


Solution

  • I think in Neo4j 1.9, you should in that case move to a server plugin or Java code, Cypher doesn't really support this before 2.0.