组合不同的 Neo4j 密码查询

Combining Distinct Neo4j Cypher Queries

我目前正在使用以下三个查询从数据库中提取一些信息。

 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

将这些查询合并为一个的正确方法是什么?

首先,我建议不要在 WHERE 子句中使用 MATCH 语法(ASCII 艺术箭头语法),除非您正在执行 NOT().

我认为您应该可以像这样使用 UNION

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

虽然我没有仔细查看查询,但可能有更优雅的方法