简体   繁体   English

Neo4j多对一关系

[英]Neo4j Many to 1 Relationship

I currently have two nodes known as "friend" and "friendOfAFriend" I want to display a many to 1 relationship where many "friends" are "friends_to" only 1 "friendOfFriend". 我目前有两个节点,分别称为“ friend”和“ friendOfAFriend”,我想显示多对一关系,其中许多“朋友”都是“ friends_to”,只有1个“ friendOfFriend”。 The only exception is I want to only have "friends" going to the "friendsOfFriend" when the "friend" can only go to 1 "friendofAFriend". 唯一的例外是,当“朋友”只能转到1个“ friendofAFriend”时,我只希望“朋友”转到“ friendsOfFriend”。 I have a csv file that pulls in the names of the "friends" and the "friendOfAFriends", but some friends can go to multiple different "friendsOfAFriends" and I want to avoid that. 我有一个csv文件,其中提取了“ friends”和“ friendOfAFriends”的名称,但是有些朋友可以转到多个不同的“ friendsOfAFriends”,因此我想避免这种情况。 Here is my current code: 这是我当前的代码:

MERGE(f:Friend{name:csv.name}) 
MERGE(fo:FriendOfAFriend{name:csv.fof}) 
OPTIONAL MATCH (f:f)-[fr:friend_to]->(fo:fo)
CREATE (f)-[newf:friend_to]->(newfo:fo)
RETURN f, new, fewfo

I would really appreciate if someone can point me in the right direction to where I only display "friends" who can only reach out to one "FriendOfAFriend" in a Many-to-1 relationship. 如果有人能向我指出正确的方向,让我仅显示“朋友”,而这些朋友只能以多对一关系与一个“ FriendOfAFriend”联系,我将不胜感激。 Right now it is currently displaying each "friend" from my csv (duplicating the friend list) going to each "FriendOfAFriend". 现在,它当前正在显示我的csv中的每个“朋友”(复制朋友列表)并转到每个“ FriendOfAFriend”。

Your data is in the CSV file, and not yet in the neo4j DB, So, to avoid storing unwanted nodes and relationships in the DB, you would have to write a query that read all the CSV data into memory, filtered out the unwanted data in memory, and finally stored the wanted data into the DB. 您的数据位于CSV文件中,而尚未在neo4j DB中,因此,为了避免在数据库中存储不必要的节点和关系,您将必须编写一个查询,将所有CSV数据读入内存,过滤掉不需要的数据在内存中,最后将所需的数据存储到DB中。 That is definitely possible to do, but may not be necessary and could cause an out-of-memory situation if the CSV file is too big. 这绝对是可以做到的,但不是必须的,如果CSV文件太大,可能会导致内存不足的情况。

I suggest that you do what you want in 2 steps. 我建议您分两步做您想做的事。 First, just create all the nodes and relationships from the CSV file, without filtering out anything (and omitting your OPTIONAl MATCH clause, which is not doing anything anyway except waste time). 首先,只需从CSV文件中创建所有节点和关系,而不过滤掉任何内容(并省略您的OPTIONAl MATCH子句,该子句除了浪费时间外什么也不做)。

Second, delete the nodes and relationships you don't want: 其次,删除不需要的节点和关系:

MATCH (f:Friend)
WHERE SIZE((f)-[:friend_to]->(:FriendOfAFriend)) > 1
FOREACH(p IN (f)-[:friend_to]->(:FriendOfAFriend) | DELETE RELATIONSHIPS(p)[0])
DETACH DELETE f;

The WHERE clause is a quick degree-ness check to efficiently find the Friend nodes with more than 1 outgoing friend_to relationship. WHERE子句是一种快速的度检查,可有效地查找具有多于1个传出的friend_to关系的Friend节点。 The FOREACH clause will delete the unwanted friend_to relationships, and the DETACH DELETE clause will delete the unwanted Friend nodes (and any remaining relationships they may have). FOREACH子句将删除不需要的friend_to关系,而DETACH DELETE子句将删除不需要的Friend节点(以及它们可能具有的任何剩余关系)。

[By the way, your data model (with both Friend and FriendOfAFriend ) seems wrong, but that is another issue.] [顺便说一下,您的数据模型(同时具有FriendFriendOfAFriend )似乎是错误的,但这是另一个问题。]

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

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