Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-14-2020 09:56 PM
I'm trying to create nodes representing each unique community, and create an edge between each particular node that belongs in community and the community node (basically, create a new Community
node for each community ID found, and then add a MEMBER_OF
relationship for each node with corresponding community ID).
I already ran the Louvain algorithm and assigned id for each community.
Is it possible somehow not to going through the entire graph in order to find all community id?
Or how to make it so that when the algorithm finds a new community id, it creates a new community node and creates MEMBER_OF
relationship with all next corresponding nodes in community.?
I am a marketer, not a developer and therefore the question can be silly.
Thank you for your attention and time
Solved! Go to Solution.
08-16-2020 01:23 PM
Create a UNIQUE CONSTRAINT for the community node to speed up the creation:
CREATE CONSTRAINT community_id ON (c:Community) ASSERT c.id IS UNIQUE
To create Community
nodes:
MATCH (u:user)
WITH collect(DISTINCT u.communityId) AS ids
UNWIND ids AS id
CREATE (c:Community{id:id})
To create MEMBER_OF
relations
CALL apoc.periodic.iterate('
MATCH (u:user)
RETURN DISTINCT u
', '
MATCH (c:Community{id:u.communityId})
CREATE (u)-[:MEMBER_OF]->(c)
', {batchSize:1000, iterateList:true})
Regards,
Cobra
08-15-2020 12:16 AM
Hello @yevheniy.derykot
What is the purpose of creating a relationship in your case? Do you want to add some properties, after, in these relationships?
Could you upload the schema of your database?
call db.schema.visualization()
Regards,
Cobra
08-15-2020 07:21 AM
I create these relationship in order to find nodes that are in two communities at the same time.
Yes, I will add properties after.
08-15-2020 02:35 PM
Can you run this query and give us the result?
MATCH (u:user) RETURN DISTINCT keys(u)
Regards,
Cobra
08-16-2020 09:56 AM
08-16-2020 01:23 PM
Create a UNIQUE CONSTRAINT for the community node to speed up the creation:
CREATE CONSTRAINT community_id ON (c:Community) ASSERT c.id IS UNIQUE
To create Community
nodes:
MATCH (u:user)
WITH collect(DISTINCT u.communityId) AS ids
UNWIND ids AS id
CREATE (c:Community{id:id})
To create MEMBER_OF
relations
CALL apoc.periodic.iterate('
MATCH (u:user)
RETURN DISTINCT u
', '
MATCH (c:Community{id:u.communityId})
CREATE (u)-[:MEMBER_OF]->(c)
', {batchSize:1000, iterateList:true})
Regards,
Cobra
08-17-2020 09:08 AM
You can't even imagine how grateful I am.
I didn’t expect you to write a completely finished code, thank you!
08-17-2020 09:10 AM
No problem, it's a pleasure
All the sessions of the conference are now available online