cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Query with optional match

lx2pwnd
Node Clone

I'm using neovis.js in my web app to connect with neo4j db where there are a node (:user) and some nodes (:Friend)
For each nodes (:Friend) there are a relationship [:FRIEND] between they and the (:user) like in the screen below

Some nodes (:Friend) are connected by a relationship [s:TAGGED_TOGETHER] like in the screen below, which have an attribute s.tagged_together

Every nodes (n:Friend) have an attribute n.timestamp.

My goal is to return every relationship [:TAGGED_TOGETHER] greater then a specific value and every relationship [:FRIEND] in a specific range of time

I write this query

MATCH x=(:fbUser)-[:FRIEND]-(f) 
OPTIONAL MATCH y=(f)-[s:TAGGED_TOGETHER]-(d:Friend) 
WITH x,f,y,s,d, case d when null then [] else [1] end as iterList  
WHERE 
(f.timestamp>'2018/01/01'  AND f.timestamp< '2019/01/01' ) AND
ALL (x in iterList WHERE 
s.tagged_together>=0) 
RETURN x,y

But in this way the query return for each node (n:Friend)all the [:TAGGED_TOGETHER] relationship.
So, I thinked to modify the query as follow:

MATCH x=(:fbUser)-[:FRIEND]-(f) 
OPTIONAL MATCH y=(f)-[s:TAGGED_TOGETHER]-(d:Friend) 
WITH x,f,y,s,d, case d when null then [] else [1] end as iterList  
WHERE 
(f.timestamp>'2018/01/01'  AND f.timestamp< '2019/01/01' ) AND
ALL (x in iterList WHERE 
s.tagged_together>=0 AND d.timestamp>'2018/01/01'  AND d.timestamp< '2019/01/01' ) 
RETURN x,y

But i think that it's incorrect because not all the node (:Friend) are connected with the (:user) although they have the timestamp in the range of time specified

How can I correct the query ?

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:

MATCH (a:User)-[:FRIEND]-(f:Friend) 
WHERE f.timestamp >= '2018/01/01' and f.timestamp <= '2019/01/01'
OPTIONAL MATCH (f)-[:TAGGED_TOGETHER]->(d:Friend)
RETURN a, f, d


View solution in original post

2 REPLIES 2

ameyasoft
Graph Maven
Try this:

MATCH (a:User)-[:FRIEND]-(f:Friend) 
WHERE f.timestamp >= '2018/01/01' and f.timestamp <= '2019/01/01'
OPTIONAL MATCH (f)-[:TAGGED_TOGETHER]->(d:Friend)
RETURN a, f, d


I add the where clause under the optional match and it works. Thanks