仅当多个属性存在于 Neo4j 中时才检查它们的值

Check for value of multiple properties only if they exist in Neo4j

我的类型为 x 的节点具有一些属性。 我试图形成一个查询,检查给定的一组属性是否存在于 x 中,如果存在,则将每个属性与一个值进行比较,然后 return 相关节点。伪代码是这样的:-

match(x)
if exist(x.a, x.b)
   then if(x.a==1 and x.b==2)
         return x
else if exist(x.a)
   then if(x.a==1)
         return x
else if exist(x.b)
   then if(x.b==1)
         return x

我试过了:-

MATCH (x) WHERE exists(x.a) and ('high' IN (x.a)) and exists(x.b) and ('high' IN (x.b)) RETURN x

和:-

match(x) where x.a='high' and x.b='high' return x

但是这两个查询的问题是,如果说 'a' 不是 x 中的 属性 而 'b' 是,它 return 是 null 而不给出结果至少基于 'b' 的值。我假设这是因为 and 子句,但我找不到替代方法。 我正在使用 neo4j 3.1.3

这可能有效:

CREATE (:X {a: "first a"})
CREATE (:X {a: "second a", b: "first b"})
CREATE (:X {b: "second b"})

MATCH (x:X)
WHERE exists(x.a)
AND exists (x.b)
AND <any other conditions>
RETURN x
UNION 
MATCH (x:X)
WHERE exists(x.a)
AND NOT exists (x.b)
AND <any other conditions>
RETURN x
UNION 
MATCH (x:X)
WHERE NOT exists(x.a)
AND exists (x.b)
AND <any other conditions>
RETURN x;

由于三种情况return相同的事情你可以指定三种情况并将它们联合在一起。

希望这对您有所帮助, 汤姆

UNION 运算符将在这种情况下提供帮助,使查询类似于:-

match(x) where x.a='high' return x union match(x) where x.b='high' return x

从上面 Tom 的回答中得到了灵感!