Bruggen Blog: Graphs are everywhere - also in Religious Texts - part 4 - Connect...
source link: https://blog.bruggen.com/2022/07/graphs-are-everywhere-also-in-religious_10.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Graphs are everywhere - also in Religious Texts - part 4 - Connect the Hadiths with the scholars
Now, we will connect the Hadiths with the scholars:
:auto MATCH (h:Hadith)
WITH h, split(replace(h.chain_indx," ",""),",") as scholarlist
CALL
{
WITH h, scholarlist
UNWIND scholarlist as scholar
MATCH (s:Scholar {scholar_indx: scholar})
MERGE (s)-[:NARRATED]->(h)
} IN TRANSACTIONS OF 100 ROWS;
And then, here's where the magic happens: using the chain_indx
field of the Hadith nodes, we can now connect pairs of Scholars together. The following query uses a super useful apoc.coll.pairsMin
function to turn an array of scholar_indx
properties into pairs of consecutive chain members.
:auto MATCH (h:Hadith)
WITH h, apoc.coll.pairsMin(apoc.coll.sort(toIntegerList(split(replace(h.chain_indx," ",""),",")))) as scholarpairs
CALL {
WITH h, scholarpairs
UNWIND scholarpairs as scholarpair
MATCH (s1:Scholar {scholar_indx: toString(scholarpair[0])}), (s2:Scholar {scholar_indx: toString(scholarpair[1])})
WHERE id(s1)<>id(s2)
MERGE (s1)-[:HADITH_CHAIN {hadith_id: h.hadith_id}]->(s2)
} IN TRANSACTIONS OF 100 ROWS;
To supplement the individual HADITH_CHAIN
relationships, I also decided to include an aggregated relationship that - instead of having a relationship between 2 scholars for every chain membership that they are part of - we just count the number of memberships and tally them up as a property on the AGGREGATED_HADITH_CHAIN
relationship. So a weight on the relationship, so to speak, that would indicate the "strength" of the relationship between the scholars.
:auto MATCH (s1:Scholar)-[r:HADITH_CHAIN]->(s2:Scholar)
WITH s1, s2, count(r) as count_of_hadiths
call {
WITH s1, s2, count_of_hadiths
create (s1)-[:AGGREGATED_HADITH_CHAIN {nr_of_hadiths: count_of_hadiths}]->(s2)
} IN TRANSACTIONS OF 100 ROWS;
This is what the result of these two queries looks like:
Now we have the graph ready for exploration.
Looking forward already!
PS: as always all the code/queries are available on github!
PPS: you can find all the parts in this blogpost on the following links
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK