构造带有条件的查询



我正在使用Spring Data Neo4j,我有一个像这样的存储库:

public interface MyNeo4jRepository extends Neo4jRepository<Object, Long> {
@Query("with ['X', 'Y','Z'] as list_labels, "
+ "$appsFilter as appsListn "
+ "MATCH (apps:) where apps.n IN appsList "
+ "MATCH (a)<-[:event]-(nodes) "
+ "WHERE any(x IN labels(nodes) WHERE x IN list_labels) "
+ "CALL apoc.path.expandConfig(nodes, { "
+ "relationshipFilter: 'R1|R2>',"
+ "labelFilter: '-l1|>l2',"
+ "maxLevel: 6,"
+ "endNodes: [apps],"
+ "uniqueness: 'NODE_PATH'}) YIELD path "
+ "unwind nodes(path) as n  "
...
}

我想用这样的条件创建这个查询:

@Query("with ['X', 'Y','Z'] as list_labels, "
+ "$appsFilter as appsListn "
+ "MATCH (apps:) where apps.n IN appsList "
+ "MATCH (a)<-[:event]-(nodes) "
+ "WHERE any(x IN labels(nodes) WHERE x IN list_labels) "
if (condition) + "WHERE  ...." else + ""
+ "CALL apoc.path.expandConfig(nodes, { "
...

是否有一种方法可以在Neo4j查询中做到这一点,或者我是否必须使用Spring可组合存储库?

我想你要找的是CASE结构

'WHERE  ....'
+
CASE 
WHEN condition1 THEN  'cypherFragement1'
WHEN condition2 THEN  'cypherFragement2'
ELSE 'cypherFragementElse'
END
+
.....

也许您可以通过提供条件判断的apoc.do.when或apoc.do.case函数重写整个密码。

最新更新