简体   繁体   English

如何使用Cypher在neo4j中“组合”两个节点和关系

[英]How to “combine” two nodes and relationships in neo4j using Cypher

I'm modeling a "tag cloud" with the graph: 我正在用该图建模“标签云”:

(t:Tag {name:'cypher'})-[:IN]->(g:TagGroup)<-[:TAGGED]-(x)

IE: A named tag is part of a "TagGroup", to which zero or more nodes are "TAGGED". IE:命名标签是“ TagGroup”的一部分,零个或多个节点被“ TAGGED”连接。 I chose this design as I want the ability to combine two or more named tags (eg "cypher" and "neo4j") so that both (Tag) s are [IN] the new (TagGroup) and the new (TagGroup) is the endpoint for the union of all nodes that were previously [TAGGED] . 我之所以选择这种设计,是因为我希望能够组合两个或多个命名标签(例如“ cypher”和“ neo4j”),以便两个(Tag)都是新的(TagGroup)和新的(TagGroup) [IN]先前[TAGGED]的所有节点的并集的端点。

My only (not very pleasing) attempt is: 我唯一(不是很令人满意)的尝试是:

match (t:Tag {name:'cypher'})-[i:IN]->(g:TagGroup),
(t2:Tag {name:'neo4j'})-[:IN]->(g2:TagGroup)<-[y:TAGGED]-(x)
create (t2)-[:IN]->(g) 
create unique (g)<-[:TAGGED]-(x)
with g2 as g2 
match (g2)<-[r]->() delete g2,r

My main issues is that it only combines two nodes, and doesn't feel very efficient (although I have no alternatives to compare it with). 我的主要问题是它仅合并了两个节点,并且效率不高(尽管我没有其他选择可与之比较)。 Ideally I'd be able to combine an arbitrary set of (Tag)s by name. 理想情况下,我可以按名称组合任意一组(Tag)。

Any ideas if this can be done with Cypher, and if so, how? 如果可以使用Cypher完成任何想法,如果可以,怎么做?

You can use labels instead of creating separate tag groups. 您可以使用标签而不是创建单独的标签组。

eg. 例如。 if tag neo4j and cypher come under tag group say XYZ then 如果标签neo4j和cypher属于标签组,请说XYZ

MERGE (a:Tag {name: "neo4j"})-[:TAGGED]->(x)
MERGE (b:Tag {name: "cypher"})-[:TAGGED]->(x)
set a :XYZ , b :XYZ

So next time you want tags of a particular group TAGGED to a particular post x 因此,下次您要将特定群组的标签TAGGED标记到特定帖子x

MATCH (a:Tag:XYZ)-[:TAGGED]->(x) return a.name

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

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