Neo4j Cypher查询为null或not null值

时间:2016-07-27 14:37:16

标签: neo4j spring-data cypher spring-data-neo4j

在我的Spring Data Neo4j项目中,我有以下实体:

@NodeEntity
public class Decision extends Commentable {

    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";

    private String name;

    @Relationship(type = DEFINED_BY, direction = Relationship.INCOMING)
    private Set<CriterionGroup> criterionGroups = new HashSet<>();

    @Relationship(type = DEFINED_BY, direction = Relationship.INCOMING)
    private Set<Criterion> criteria = new HashSet<>();

...

}

@NodeEntity
public class Criterion extends Authorable {

    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";

    private String name;

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private CriterionGroup group;

    @Relationship(type = DEFINED_BY, direction = Relationship.OUTGOING)
    private Decision owner;

...

}

@NodeEntity
public class CriterionGroup extends Authorable {

    private final static String DEFINED_BY = "DEFINED_BY";
    private final static String CONTAINS = "CONTAINS";

    private String name;

    @Relationship(type = DEFINED_BY, direction = Relationship.OUTGOING)
    private Decision owner;

    @Relationship(type = CONTAINS, direction = Relationship.OUTGOING)
    private Set<Criterion> criteria = new HashSet<>();

...

}

我有以下SDN存储库方法:

@Query("MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion) WHERE id(d) = {decisionId} and c.name = {name} RETURN c")
Criterion findCriterionDefinedByDecisionByName(@Param("decisionId") Long decisionId, @Param("name") String name);

根据此查询,我可以获得属于Criterion且具有特定名称的Decision

我的域模型中的标准可能(或可能不属于)CriterionGroup

我需要扩展此查询,以便再添加一个条件来检查与此Criterion相关联的CriterionGroup。换句话说,我需要将Criterion以特定{nameId}的特定{name}返回给提供的{criterionGroupId},该属性(或不是空值的情况下)。如果{criterionGroupId} == null,我需要找到不属于任何CriterionGroup的Criterion。

我需要这样的东西:

@Query("MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion)...????????...... WHERE id(d) = {decisionId} and c.name = {name} RETURN c")
Criterion findCriterionDefinedByDecisionByName(@Param("decisionId") Long decisionId, @Param("name") String name, @Param("criterionGroupId") Long criterionGroupId);

请帮我写这个查询。

1 个答案:

答案 0 :(得分:1)

这应该有用,并且除非你每CriterionGroupsCriterion成千上万,否则不应该太慢:

MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion)
WHERE id(d) = {decisionId} 
  AND c.name = {name}
OPTIONAL MATCH (c)<-[:CONTAINS]-(cg:CriterionGroup)
WITH c, extract(g IN collect(cg) | id(g)) AS cgIds
WHERE CASE
        WHEN {criterionGroupId} IS NULL THEN size(cgIds) = 0
        ELSE {criterionGroupId} IN cgIds
      END
RETURN c

或者,您可以在Repository中使用2种方法,使用

直接管理每个案例
MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion)
WHERE id(d) = {decisionId} 
  AND c.name = {name}
  AND NOT (c)<-[:CONTAINS]-(:CriterionGroup)
RETURN c

criterionGroupId为空时,

MATCH (d:Decision)<-[:DEFINED_BY]-(c:Criterion),
      (c)<-[:CONTAINS]-(cg:CriterionGroup)
WHERE id(d) = {decisionId} 
  AND c.name = {name}
  AND id(cg) = {criterionGroupId}
RETURN c

否则。