本文介绍了如何使用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'}.

我尝试过

首先,对每个审阅使用一个密码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个PRODUCT {name:'X phone'}

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

我尝试使用MERGE来创建/重复使用该产品,他们进行了MATCH + 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个PRODUCT并将3个REVIEW连接到该PRODUCT.但是,我必须使用两倍的密码.

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ạnhQuyếtNguyễn的答案,我可以为每个带有2个MERGE子句的评论样本做1个密码.如果不存在,则首先进行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创建或重用部分模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:53