如何使用 MERGE 创建或重用模式的一部分? [英] How to use MERGE to create or reuse a part of pattern?

查看:21
本文介绍了如何使用 MERGE 创建或重用模式的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有多个 REVIEW 节点的节点 PRODUCT.如果产品不存在,我将创建节点 PRODUCE,然后与 REVIEW 绑定.

I have node PRODUCT with multiple REVIEW node. I would create node PRODUCE if the product is not exist, then bind with REVIEW.

例如,我想要一个 PRODUCE 节点 {name:'X phone'} 和 3 REVIEW {content:'best phone ever'}, {content:'worst phone ever'}, {content:'nope'}.

For the example, I want a PRODUCE node {name:'X phone'} with 3 REVIEW {content:'best phone ever'}, {content:'worst phone ever'}, {content:'nope'}.

我试过了

首先,对每个 REVIEW 使用一个密码 MERGE.

First, use one cypher MERGE for each REVIEW.

MERGE(product:PRODUCT{name:'X phone'})-[:RATE]-(review:REVIEW{content:'best phone ever'})

MERGE(product:PRODUCT{name:'X phone'})-[:RATE]-(review:REVIEW{content:'worst phone ever'})

MERGE(product:PRODUCT{name:'X phone'})-[:RATE]-(review:REVIEW{content:'nope'})

它不起作用,将创建 3 个产品 {name:'X phone'}

It did not work and will create 3 PRODUCT {name:'X phone'}

我尝试使用 MERGE 来创建/重用产品,它们匹配 + 合并评论.(每条评论 2 个密码)

I tried to use MERGE to create/reuse the PRODUCT, them MATCH + MERGE the review. (2 cypher for each review)

// create/reuse PRODUCT
MERGE(product:PRODUCT{name:'X phone'})  
// create/reuse review
MATCH (product:PRODUCT{name:'X phone'}) MERGE (product)-[:RATE]-(review:REVIEW{content:'nope'})

// create/reuse PRODUCT
MERGE(product:PRODUCT{name:'X phone'})  
// create/reuse review
MATCH (product:PRODUCT{name:'X phone'}) MERGE (product)-[:RATE]-(review:REVIEW{content:'best phone ever'})

// create/reuse PRODUCT
MERGE(product:PRODUCT{name:'X phone'})  
// create/reuse review
MATCH (product:PRODUCT{name:'X phone'}) MERGE (product)-[:RATE]-(review:REVIEW{content:'worst phone ever'})

第二个解决方案有效,它只创建 1 个产品并将 3 个评论连接到该产品.但是,我必须使用两倍数量的密码.

Second solution worked, it only create 1 PRODUCT and connect 3 REVIEW to that PRODUCT. But, I have to use as twice as number of cypher.

请问有没有更好的办法解决我的问题?

I would like to ask if there is any better way for my problem ?

解决方案

我不知道我可以用 MERGE 链接 MERGE

I did not know that I can chain MERGE with MERGE

感谢@Mạnh Quyết Nguyễn 的回答,我可以为每个评论样本执行 1 个密码,链接 2 个 MERGE 子句.第一个 MERGE 如果不存在则创建产品,如果不存在则第二个 MERGE 创建评论.

thank to @Mạnh Quyết Nguyễn answer, I can do 1 cypher per review sample that chain 2 MERGE clause. First MERGE create a product if not exist, second MERGE create review if not exist.

MERGE (product:PRODUCT{name:'X phone'}) MERGE (product)-[:RATE]-(review:REVIEW{content:'worst phone ever'})

推荐答案

您随后的匹配/合并是多余的.你可以这样做:

Your subsequent match/merge is redundant. You can do:

// create/reuse PRODUCT
MERGE (product:PRODUCT {name:'X phone'})  
MERGE (product)-[:RATE]-(:REVIEW {content:'nope'})
MERGE (product)-[:RATE]-(:REVIEW {content:'best phone ever'})
MERGE (product)-[:RATE]-(:REVIEW {content:'worst phone ever'})

这篇关于如何使用 MERGE 创建或重用模式的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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