我正在尝试使用neo4j密码dsl(https://github.com/neo4j-contrib/cypher-dsl)但它不知道如何用OR在where子句中添加标签。我想要所有有运动员或学生标签的节点。请帮我复习一下。
match (n) where (n:Student) or (n:Sportsman) with n return n.Name
我将把您的密码查询转换为UNION ALL,然后执行密码dsl语句。
此密码查询:
match (n) where (n:Student) or (n:Sportsman) with n return n.Name
相当于:
match (n:Student)
return n.Name
UNION ALL
match (n:Sportsman)
return n.Name
下面是密码dsl:
var node1 = Cypher.node("Student").named("n")
var statement1 = Cypher.match(node1)
.returning(n.property("Name"))
.build();
var node2 = Cypher.node("Sportsman").named("n")
var statement2 = Cypher.match(node2)
.returning(n.property("Name"))
.build();
var unionStatement = Cypher.unionAll(statement1, statement2)
assertThat(cypherRenderer.render(unionStatement))
.isEqualTo(
"MATCH (n:`Student`) RETURN n.Name UNION ALL MATCH (n:`Sportsman`) RETURN n.Name");
下面是一个工作片段:
var node = Cypher.anyNode().named("n");
var cypher = Cypher.match(node)
.where(node.hasLabels("Student")).or(node.hasLabels("Sportsman"))
.returning(node.property("name"))
.build();
System.out.println(Renderer.getDefaultRenderer().render(cypher));
结果:
MATCH (n) WHERE (n:`Student` OR n:`Sportsman`) RETURN n.name
一般来说,我建议查看密码dsl代码库中的示例:https://github.com/neo4j-contrib/cypher-dsl/blob/main/neo4j-cypher-dsl-examples/neo4j-cypher-dsl-examples-core/src/test/java/org/neo4j/cypherdsl/examples/core/CypherDSLExamplesTest.java