Reputation: 123
The data is in the following format
{"domain":"sdcastroverde.com","cat_labels_in":"Top/World/Galego/regional/Galicia/Lugo/municipalities/Castroverde"}
When I run a Cypher query
MATCH (n:Dmoz)
return split(n.cat_labels_in,'/') as data limit 10
I get the answer
["Top","World","Galego","regional","Galicia","Lugo","municipalities","Castroverde"]
I do not want to return the data, I want to use UNWIND on it so that I can write the "Tags" to the db as (:Tags{name:"Tag"}) and add relationship in the following fashion
(:Domain{name:"somedomain.com"})-[LINKS_TO]->((:Tags{name:"Tag"})) #first tag
(:Tags{name:"Tag"})-[LINKS_TO]->((:Tags{name:"Tag"})) #first tag to second tag and so on
(:Tags{name:"Tag"})-[LINKS_TO]->((:Domain{name:"anotherdomain.com"}))
Something like this Neo4j: Split string and get position but not from a CSV file.
Upvotes: 1
Views: 279
Reputation: 11216
Is something like this what you are looking for?
// start with the sample data
WITH {domain:"sdcastroverde.com",cat_labels_in:"Top/World/Galego/regional/Galicia/Lugo/municipalities/Castroverde"} as node
// split the labels into tags
WITH node, split(node.cat_labels_in,'/') as tags
// unwind the tags and make the chain of tags
UNWIND range(0, size(tags)-2) as i
MERGE (a:Tags {name: tags[i] })
MERGE (b:Tags {name: tags[i+1] })
MERGE (a)-[:LINKS_TO]->(b)
// prepend the tag chain with the starting domain
WITH node, tags
MATCH (a:Tags {name: tags[0]})
MERGE (:Domain{name:"somedomain.com"})-[:LINKS_TO]->(a)
// append the tag chain with the next domain
WITH node, tags
MATCH (a:Tags {name: tags[size(tags)-1]} )
MERGE (a)-[:LINKS_TO]->(:Domain {name:"anotherdomain.com"} )
Second solution per follow up question. The key here with mixed delimiters is to make a single collection out of the string with mixed delimiters first and then simply carry on as before.
//Start with some test data
WITH {domain:"sdcastroverde.com",cat_labels_in:"Top/World/Galego|Test/regional/Galicia|Test/Lugo/municipalities/Castroverde"} as node
// split up the list on forward slash
UNWIND split(node.cat_labels_in,'/') as tags
// for each item in the first collection attempt to split on |
UNWIND split(tags,'|') as tag
// recombine the split nodes into a single collection
WITH node, collect(tag) as tags
// then carry on as before in the above solution
UNWIND range(0, size(tags)-2) as i
MERGE (a:Tags {name: tags[i] })
MERGE (b:Tags {name: tags[i+1] })
MERGE (a)-[:LINKS_TO]->(b)
WITH node, tags
MATCH (a:Tags {name: tags[0]})
MERGE (:Domain{name:"somedomain.com"})-[:LINKS_TO]->(a)
WITH node, tags
MATCH (a:Tags {name: tags[size(tags)-1]} )
MERGE (a)-[:LINKS_TO]->(:Domain {name:"anotherdomain.com"} )
Upvotes: 1