在cypher

时间:2017-01-23 21:20:02

标签: neo4j cypher graph-databases

我希望合并两个具有相同名称的列中的实体。 下面是我的数据集示例,我希望只创建一个节点,并将所有关系链接到此节点。

Entity0      | Entity1    | Relationship | 
__________________________________________
Jamie Dimon  | Mit Romney  | friends
__________________________________________
Jamie Dimon  | Jamie Dimon | colleague
__________________________________________
Mason White  | Jamie Dimon | sibling

使用merge我可以从每列中选择唯一的,并为Jamie Dimon而不是4个节点创建2个节点。但是我想为Jamie Dimon创建一个节点。

到目前为止我的查询结构:

LOAD CSV WITH HEADERS FROM 'file:/final_neo4j.csv' AS line 
MERGE (e0:Entity0 {node: line.relation_first, name: line.entities_0_name, entity: line.entities_0_entity, confidence: toFloat(line.entities_0_confidence) }) 
WITH line, e0 
MERGE (e1:Entity1 {node: line.relation_second, name: line.entities_1_name, entity: line.entities_1_entity, confidence: toFloat(line.entities_1_confidence) })
WITH e0, e1, line 
MERGE (e0)-[r:Rel {name : line.relation_relation, confidence: toFloat(line.relation_confidence), url: line.url }]->(e1) 
RETURN e0,r,e1

此致 Nrithya

1 个答案:

答案 0 :(得分:1)

首先,为唯一标识Entity节点的属性定义唯一性约束。让我们使用name

CREATE CONSTRAINT ON (e:Entity) ASSERT e.name IS UNIQUE;

这将创建一个模式约束,并确保不能向数据库添加重复节点(由标签为Entity且属性name具有相同值的两个节点定义)。

然后,当MERGE仅在SET之后合并该属性和MERGE属性时:

LOAD CSV WITH HEADERS FROM 'file:/final_neo4j.csv' AS line 
MERGE (e0:Entity {name: line.Entity0})
ON CREATE SET e0.confidence = toFloat(line.entities_0_confidence)
MERGE (e1:Entity {name: line.Entity1})
ON CREATE SET e1.confidence = toFloat(line.entities_1_confidence)
MERGE (e0)-[r:REL {name:line.Relationship]->(e1)
RETURN *