2
votes

I am currently using the following three queries to pull some information from the database.

 MATCH (u:User { id: "1" }), (x:Label)  
 WHERE (u)-[:SEES]->(x) 
RETURN collect(x) as labels

 MATCH (u:User { id: "1" }), (x:Label)  
 WHERE (u)-[:SEES]->(x)    
   AND NOT((x)-[:CLASSIFIES]->())    
   AND NOT(()-[:CLASSIFIES]->(x)) 
RETURN collect(x) as uniquelabels

 MATCH (u:User { id: "1" }), p=(a:Label)-[:CLASSIFIES*]->(b:Label)  
 WHERE ALL(n IN nodes(path) WHERE ((u)-[:SEES]->(n)))
   AND NOT(()-[:CLASSIFIES]->(a))    
   AND NOT((b)-[:CLASSIFIES]->())   
  WITH collect(extract(n IN nodes(p) | n.id)) as paths 
RETURN distinct paths

What is the proper way to combine these queries into one?

1

1 Answers

1
votes

First off, I would suggest not using MATCH syntax (the ASCII-art arrow syntax) in the WHERE clause unless you're doing a NOT().

I think you should be able to use UNION like this:

 MATCH (u:User { id: "1" })-[:SEES]->(x:Label)  
RETURN extract(n IN collect(distinct x) | n.id) as labels
   UNION
 MATCH (x:Label)  
 WHERE NOT((x)-[:CLASSIFIES]-())    
RETURN extract(n IN collect(distinct x) | n.id) as uniquelabels
   UNION
 MATCH (u:User { id: "1" }), p=(a:Label)-[:CLASSIFIES*]->(b:Label)  
 WHERE ALL(n IN nodes(path) WHERE ((u)-[:SEES]->(n)))
   AND NOT(()-[:CLASSIFIES]->(a))    
   AND NOT((b)-[:CLASSIFIES]->())   
 RETURN DISTINCT collect(extract(n IN nodes(p) | n.id))

There might be a more elegant way to do it, though, I haven't looked at the queries too closely