数据采用以下格式
{"domain":"sdcastroverde.com","cat_labels_in":"Top/World/Galego/regional/Galicia/Lugo/municipalities/Castroverde"}
当我运行Cypher查询
时MATCH (n:Dmoz)
return split(n.cat_labels_in,'/') as data limit 10
我得到答案
["Top","World","Galego","regional","Galicia","Lugo","municipalities","Castroverde"]
我不想返回数据,我想在上面使用Undind,以便我可以将"标签"写入db as(:tags {name:" tag"}(,并以以下方式添加关系
(: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"}))
类似于此neo4j的东西:拆分字符串并从CSV文件中获取位置。
是您要寻找的东西吗?
// 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"} )
第二个解决方案每个后续问题。使用混合定义器的钥匙是先用混合的定义器从字符串中进行单个集合,然后像以前一样继续进行。
//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"} )