neo4j cypher 更新现有节点或创建新节点 [英] neo4j cypher update existing node or create new node

查看:104
本文介绍了neo4j cypher 更新现有节点或创建新节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 900 万个节点和 1200 万个关系的图表.对于图中的每个节点,每个相应节点都有一个属性子集,通过标签形成节点的唯一标识.该图由各种数据源更新,这些数据源扩充了图中的现有节点,或者如果节点不存在则创建新节点.我不想在更新期间根据图中唯一的一组属性创建重复项.

I have a graph with approximately nine million nodes, and twelve million relationships. For each of the nodes within the graph there is a subset of properties for each respective node which forms a unique identity for the node, by Label. The graph is being updated by various data sources which augment existing nodes within the graph, or create new nodes if the nodes don't exist. I don't want to create duplicates according to the unique set of properties within the graph during the update.

例如,我在图中有 People,他们的唯一性由他们的名字和姓氏决定.下面的代码是创建两个不同的人:

For example, I have People in the graph, and their uniqueness is determined by their first name and last name. The following code is to create two distinct people:

CREATE (p:Person{first:"barry",last:"smith",height:187});
CREATE (p:Person{first:"fred",last:"jones",language:"welsh"});

后来,我从数据源之一收到以下数据记录(每行一条):

Later, from one of the data sources I receive the following data records (one per line):

first: "fred", last: "lake", height: 201
first: "barry", last: "smith", language: "english"
first: "fred", last: "jones", language: "welsh", height: 188
first: "fred", last: "jones", eyes: "brown"
first: "barry", last: "smith"

更新图表后,我想要以下节点:

After updating the graph I want to have the following nodes:

(:Person{first:"fred",last:"jones",language:"welsh",height:"188,eyes:"brown"})
(:Person{first:"barry",last:"smith",language"english",height:187})
(:Person{first:"fred",last"lake",height:201})

我正在尝试制定一个可以进行这种更新的 MERGE 查询.我想出了以下方法:

I'm trying to formulate a MERGE query which can do this kind of update. I have come up with the following approach:

  • MERGE 开始,它使用唯一性属性(示例中的 firstlast)来查找或创建初始节点;
  • 然后执行包含传入记录中定义的每个属性的 SET.
  • Start with a MERGE that uses the uniqueness properties (first and last from the example) to find or create the initial node;
  • Then do a SET containing each property defined in the incoming record.

因此,对于上面给出的三个示例记录:

So, for the three examples records given above:

MERGE (p:Person{first:"fred",last:"lake"}) SET p.height = 201;
MERGE (p:Person{first:"barry",last:"smith"}) SET p.language = "english";
MERGE (p:Person{first:"fred",last:"jones"}) SET p.language = "welsh", p.height = 188;
MERGE (p:Person{first:"fred",last:"jones"}) SET p.eyes = "brown";
MERGE (p:Person{first:"barry",last:"smith"});

我已经尝试过这个并且它有效,但我很想知道这是否是确保基于一组属性的节点的唯一性并允许附加信息的最佳方式(最有效...)随着时间的推移更新会添加(或不添加)?

I've tried this out and it works, but I'm curious to know whether this is the best way (most efficient...) to ensure uniqueness in the nodes based on a set of properties, and allow additional information to be added (or not) as updates come in over time?

推荐答案

只是一种天真的方法:如果您运行 MERGE 并且只是创建或更新它会怎样?

Just a naive approach: what if you run a MERGE and just create or update it?

给定您的记录列表,将每条记录视为一张地图:

Given your list of records, consider each record as a map:

{ first: "fred", last: "lake", height: 201 }
{ first: "barry", last: "smith", language: "english" }
{ first: "fred", last: "jones", language: "welsh", height: 188 }
{ first: "fred", last: "jones", eyes: "brown" }
{ first: "barry", last: "smith" }

然后以参数方式编写查询:

Then write your query in a parametric way:

MERGE (p:Person{ first: { map }.name, last: { map }.last }
ON CREATE SET n = { map }
ON MATCH  SET n += { map }

查询描述:

我在 页面的控制台中运行了一些查询上面链接MERGE ON MATCH 并且它似乎将现有属性更新为新值.我运行的查询如下:

I've run some queries in console of the page linked above with a MERGE ON MATCH and it seems to update existing properties to new values. The queries I've run are the following:

MATCH (peter { name: 'Peter' }) RETURN peter
MERGE (peter { name: 'Peter' }) ON MATCH SET peter += { hungry: TRUE , position: 'Entrepreneur' }
MATCH (peter { name: 'Peter' }) RETURN peter
// added two new properties here
MERGE (peter { name: 'Peter' }) ON MATCH SET peter += { hungry: FALSE , position: 'Entrepreneur' }
MATCH (peter { name: 'Peter' }) RETURN peter
// hungry is now false in here

这篇关于neo4j cypher 更新现有节点或创建新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆