cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with optional match

lx2pwnd
Node Clone

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

2X_e_e09a0ede719f0c27e318002d1c8b85045f26f12f.png

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 friend.name

In a first attempt, I wrote this query:

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

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 (p)-[:TAGGED]-(f:Friend)
OPTIONAL MATCH y=(:fbUser)<-[:TAGGED]-(q)
WITH x,p,f,y,q, , case q when null then else [1] end as iterList
WHERE
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')
RETURN x,y

How can I solve it?

3 REPLIES 3

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)
WHERE
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

MATCH (p)
WHERE
p.nodeDegree>=0 AND p.timestamp>'2020-02-01' AND p.timestamp<'2020-02-29'
WITH  
	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?

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.