cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to create node representing each unique community?

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

1 ACCEPTED SOLUTION

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

View solution in original post

7 REPLIES 7

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

I create these relationship in order to find nodes that are in two communities at the same time.
Yes, I will add properties after.

Can you run this query and give us the result?

MATCH (u:user) RETURN DISTINCT keys(u)

Regards,
Cobra

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

You can't even imagine how grateful I am.

I didn’t expect you to write a completely finished code, thank you!

No problem, it's a pleasure