neo4j 如何将条件存储为节点属性或关系属性中的字符串,并在查询中使用它?

zvms9eto  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(114)
create (bird:Class {name:'bird'})-[:isSubClassOf]->(animal:Class {name:'animal'})
create (animal)-[:hasOrgan {condition:'Exists (({x})-[:isInstanceOf]->(bird))'}]->(wing:organ {name:'wing'})
create (x:bird:instance {name:'pigeon_1'})-[:isInstanceOf]->(bird)

如何创建查询来读取条件表达式的值,并根据其值MATCH(bird:Class {name:'bird'})-[:isSubClassOf]->(animal:Class {name:'animal'})决定是否创建(pigeon_1)-[:hasOrgan]->(:wing)
我创建了以下查询,但得到了一个错误,我应该如何修复它?

MATCH (animal)-[hasOrgan:hasOrgan]->(wing:organ)
WHERE hasOrgan.condition IS NOT NULL
WITH x, hasOrgan, wing, hasOrgan.condition AS condition
CALL apoc.cypher.doIt('RETURN ' + condition + ' AS value', {x: x, wing: wing})
YIELD value
WITH x, hasOrgan, wing, value AS conditionValue
WHERE conditionValue
CREATE (x)-[:hasOrgan]->(wing)
3ks5zfa0

3ks5zfa01#

以下查询可能对您有用。它假设您首先更改数据模型,将非常复杂的关系模式[:hasOrgan {condition:'Exists (({x})-[:isInstanceOf]->(bird))'}]替换为简单得多的[:hasOrgan {ifClass: 'bird'}]

MATCH (cls:Class)-[hasOrgan:hasOrgan]->(organ:organ)
WHERE hasOrgan.ifClass IS NOT NULL
MATCH p=(inst:instance)-[:isInstanceOf]->()-[:isSubClassOf*0..]->(cls)-[:isSubClassOf*0..]->(clsN)
WHERE
  NOT (clsN)-[:isSubClassOf]->() AND
  ANY(c IN TAIL(NODES(p)) WHERE hasOrgan.ifClass = c.name)
MERGE (inst)-[:hasOrgan]->(organ)

查询执行此操作的顺序是:
1.找到每个具有ifClass属性的hasOrgan关系以及关联的类cls和器官organ
1.找到每个instance的所有类(直接或间接)在类cls中,如果它不属于hasOrgan.ifClass指定的类,则过滤掉一个示例。
1.使用MERGE创建hasOrgan关系(如果不存在)。

相关问题