我正在使用 JSON APOC 插件从包含列表的 JSON 创建节点,并且我正在尝试创建其标签作为列表中的元素列出的节点:
{
"pdf":[
{
"docID": "docid1",
"docLink": "/examplelink.pdf",
"docType": "PDF"
}
],
"jpeg":[
{
"docID": "docid20",
"docLink": "/examplelink20.pdf",
"docType": "JPEG"
}
],
...,}
我想迭代文档类型(pdf、jpeg)并将标签设置为列表中的 docType 属性。现在我必须为每个文档类型列表执行单独的块(jpeg:[],pdf:[]):
WITH "file:////input.json" AS url
CALL apoc.load.json(url) YIELD value
UNWIND value.pdf as doc
MERGE (d:PDF {docID: doc.docID})
我想循环遍历文档类型列表,为每个文档类型创建节点,并将标签作为列表名称(pdf)或节点的文档类型名称(PDF)。比如:
WITH "file:////input.json" AS url
CALL apoc.load.json(url) YIELD value
for each doctypelist in value
for each doc in doctype list
MERGE(d:doc.docType {docID: doc.docID})
或者
WITH "file:////input.json" AS url
CALL apoc.load.json(url) YIELD value
for each doctypelist in value
for each doc in doctype list
MERGE(d {docID: doc.docID})
ON CREATE SET d :doc.docType
Cypher 目前不支持此功能。要设置标签,您必须将其硬编码到 Cypher 中。您可以使用过滤器或多个匹配来以繁琐的方式执行此操作,但如果不允许您在 Neo4j 数据库中安装任何插件,我建议您只在类型上放置索引,或者使用节点+关系而不是标签。 (有效的文档类型有很多,所以如果你必须支持所有类型,纯 Cypher 会让它变得非常痛苦。)
但是使用 APOC,有一个专门用于此目的的 程序
apoc.create.addLabels
CREATE (:Movie {title: 'A Few Good Men', genre: 'Drama'});
MATCH (n:Movie)
CALL apoc.create.addLabels( id(n), [ n.genre ] ) YIELD node
REMOVE node.genre
RETURN node;
我做了这样的事情来从数据框中获取节点标签:
for index, row in df.iterrows():
label = row.label # for node label
records, summary, keys= driver.execute_query(
"create (c:"""+ label + """ {name :$cname})",
cname=row.Name,
database_="neo4j",)