Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-13-2021 09:18 AM
Say I have a group of nodes called "books" from some match statement like "MATCH(books:Book{year:2019})." I want to create a relationship between every pair of nodes in this group. So say "books" consists of book1, book2, and book3. I want to create these relationships:
book1
^ ^
| \
V V
book2<->book3
How would I do this?
08-13-2021 10:36 AM
Hi, I am not sure that is recommended but..
You can do this:
MATCH (b:Books {year: 2019})
MATCH (b1:Books {year: 2019})
where id(b)<>id(b1)
MERGE (b1)-[:MY_REL]->(b)
MERGE (b1)<-[:MY_REL]-(b)
return *
Results:
08-15-2021 12:40 AM
I don't have the name of it right now, but look in the apoc documentation with the keyword link, there is a function explicitly build for that case.
08-16-2021 05:00 PM
Try this:
MATCH (b:Books {year: 2019})
with b order by id(b) ASC
with collect(b) as b2
CALL apoc.nodes.link(b2, 'MY_REL')
return b2
08-17-2021 12:34 AM
as @SamChalvet said "I'm not sure that is recommended", I would like to add: it is rarely usable and make no sense.
It just slow down any performance without any advantages. And doubled both directed relationship is to avoid anyway.
The "least bad solution" is to create a top-node with label "Book_Label" and link all the book node to it with a "is_a" relationship. But it doesn't help a lot.
You will already have an ordered list of all books with the query
Match (b:Book)
Return b order by id(b)
Explain what you want to achieve.
All the sessions of the conference are now available online