Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Problem with optional match

Node Clone

Consider the following schema, where green nodes are of type Post or Comment and pink node is of type fbUser .


This green node could have a relathionship like follow where the yellow node are of type Friend

In the same way you can consider this schema

Where the blue nodes are of Post type

The query that I am trying to write goes as follows:

I'm trying to match all nodes Post or Comment in a specific range of time and optionally related to each Post or `Comment nodes all the

In a first attempt, I wrote this query:

MATCH x=(:fbUser)-[:PUBLISHED]-(p)
WITH x,p,f
p.nodeDegree>=0 AND p.timestamp>'2016-03-23' AND p.timestamp<'2020-03-23'

This way, it doesn't return the (:fbUser)<-[:TAGGED]-(:Post)

I can't find a way to return both of paths because not in every range of time I have (:fbUser)<-[:TAGGED]-(:Post) or (:fbUser)-[:PUBLISHED]-(p:Post) or (:fbUser)-[:PUBLISHED]-(Comment)

I tried in this way without any results:

MATCH x=(:fbUser)-[:PUBLISHED]-(p)
OPTIONAL MATCH y=(:fbUser)<-[:TAGGED]-(q)
WITH x,p,f,y,q, , case q when null then else [1] end as iterList
p.nodeDegree>=0 AND
p.timestamp>'2016-03-23' AND p.timestamp<'2020-03-23' AND ALL (x in iterList WHERE
q.timestamp>'2016-02-23' AND q.timestamp<'2020-02-22')

How can I solve it?


Not quite seeing an OPTIONAL MATCH problem here, it looks like you're having a little trouble figuring how how to return elements of your query, however.

For one, keep WHERE closes associated with the MATCH or OPTIONAL MATCH they belong to, in order to ensure indexes are being used correctly, and that you're getting correct results.

To avoid cardinality issues, try collecting the tag patterns per post. A pattern comprehension should do the trick.

Give this a try:

MATCH x=(:fbUser)-[:PUBLISHED]-(p)
p.nodeDegree>=0 AND p.timestamp>'2016-03-23' AND p.timestamp<'2020-03-23'
WITH x, p, [tagged=(p)-[:TAGGED]-(:Friend) | tagged] as f
RETURN x, p, f

Thanks for your answer. I read your solution and it's works but it doesn't include this path (:fbUser)<-[:TAGGED]-(q). Is it possible include it ?

I tried in this way, but I don't know if it's correct

p.nodeDegree>=0 AND p.timestamp>'2020-02-01' AND p.timestamp<'2020-02-29'
	p as p, 
    [published=(p)<-[:PUBLISHED]-(:fbUser) | published] as r,
    [ftagged=(p)-[:TAGGED]-(:Friend) | ftagged] as f, 
    [utagged=(p)<-[:TAGGED]-(:fbUser) | utagged] as u
RETURN r,f,u,p

You want (:fbUser)<-[:TAGGED]-(q), but q doesn't exist in the query. Isn't this meant to be an extension from one of the other patterns? If so, my guess is this is meant to extend from the :fbUser from one of the others in the query, but which one? Is it the :fbUser who published p, or is the :fbUser who tagged p?