Chet
Chet

Reputation: 19829

Neo4j -- WHERE, ORDER BY, and LIMIT after UNION

I'm trying to do a query that involves a UNION, but filters with a WHERE, ORDER BY, and LIMIT after the union.

The basic idea is to find all posts STARRED or POSTED by users that another user FOLLOWS. For example, the posts s and p are the posts of interest.

MATCH (a:USER {id:0})-[:FOLLOWS]->(b:USER),
      (b)-[:STARRED]->(s:POST),
      (b)-[:POSTED]->(p:POST)

I'd like to return the union of the id property of both s and p after filtering, sorting, and limiting the results. Any relevant indexes to create that make this query efficient would be helpful as well.

If u is the union of s and p, I'd want to do something like:

WHERE u.time > 1431546036148
RETURN u.id ORDER BY u.time SKIP 0 LIMIT 20

I don't know how to get u from s and p, and I don't know what indexes to create to make this query efficient.

Upvotes: 2

Views: 506

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20175

You can use multiple relationships types so you'll not have to do UNION.

I guess the time property is on the POST node :

MATCH (user:USER {id:0})-[:FOLLOWS]->(friend:USER)
MATCH (friend)-[:STARRED|:POSTED]->(p:POST)
WHERE p.time > 1431546036148
RETURN p
ORDER BY p.time
LIMIT 25

Upvotes: 4

Related Questions