简体   繁体   English

Neo4j和cypher:如何匹配/创建路径的特定部分?

[英]Neo4j and cypher: how to match/create specific parts of a path?

I have pre-existing nodes a and b , and want to create a path 我有预先存在的节点ab ,并想要创建一个路径

(a) <-[:FROM]- (c:Comp) -[:TO]-> (b)

Unless it already exists. 除非已经存在。 create unique doesn't quite work here, because if we already have create unique在这里不太可行,因为如果我们已经拥有

(a) <-[:FROM]- (d:Comp) -[:TO]-> (e)

Then d will get reused as c , and only the relationship (c) -[:TO]-> (b) will get created. 然后d将作为c重用,并且仅将创建关系(c) -[:TO]-> (b) I want to create a new :Comp node in this case. 在这种情况下,我想创建一个新的:Comp节点。

Whether or not the :Comp node was just created, I subsequently want to create unique a relationship from it. 无论是否刚刚创建了:Comp节点,我随后都希望从中create unique的关系。

I've come up with two possible solutions that I don't particularly like: 我提出了两种我不太喜欢的解决方案:

I think I could do it with optional relationships, something like 我想我可以通过可选的关系来做到这一点,例如

match (a) <-[r1?:FROM]- (c:Comp) -[r2?:TO]-> (b)
where r1 is null or r2 is null
create (a) <-[:FROM]- (d:Comp) -[:TO]-> (b)

But I'm not sure how I'd create the extra relationship if the :Comp node already exists. 但是我不确定如果:Comp节点已经存在,我将如何创建额外的关系。

Another option is to have properties on c uniquely identifying a and b , along the lines of 另一种选择是在c上具有沿b的行唯一标识ab属性。

(a) <-[:FROM]- (c:Comp { from: a.uuid, to: b.uuid }) -[:TO]-> (b)

So that create unique won't start with a partial match. 因此, create unique不会从部分匹配开始。 But having redundant data lying around is a smell. 但是散布着冗余数据是一种气味。

I created what I think is your model in a console: http://console.neo4j.org/?id=l6q3q0 我在控制台中创建了我认为是您的模型的模型: http : //console.neo4j.org/?id=l6q3q0

Perhaps try this: 也许试试这个:

START a=node:node_auto_index(name='a'), b=node:node_auto_index(name='b')
WHERE NOT (a<-[:FROM]-(:Comp)-[:TO]->b)
CREATE a<-[:FROM]-(c:Comp), c-[:TO]->b

I haven't tried yet, but it looks like MERGE can do this as of 2.0.0-rc1. 我还没有尝试过,但是从2.0.0-rc1开始,似乎MERGE可以做到。 http://docs.neo4j.org/chunked/milestone/query-merge.html http://docs.neo4j.org/chunked/milestone/query-merge.html

I'll want something like 我想要类似的东西

MERGE (a) <-[:FROM]- (c:Comp) -[:TO]-> (b)
CREATE UNIQUE (c) -[:SUGGESTED_BY]-> (x:Algo)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM