UNWIND 从 JSON 文件加载的多个不相关的数组

UNWIND multiple unrelated arrays loaded from JSON file

我正在尝试通过一次调用 apoc.load.json() 来展开多个数组属性。我的版本不能完全工作:一些关系没有加载。我的猜测是这是由于通过 WITH 命令输出的管道。如果我 运行 为每个基于数组的 属性 分别展开,我可以全部加载,但我很好奇如何一起完成。

感谢任何见解和指点=)

//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity,

WITH c, class.items AS items, class.companions AS companions, class.locations AS locations
UNWIND items AS item
UNWIND companions AS companion
UNWIND locations AS location

MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)

JSON 文件中的示例条目:

 {
    "name": "KNIGHT",
    "strength": [75,100],
    "intelligence": [40,80],
    "dexterity": [40,85],
    "items": [
        "SWORD",
        "SHIELD"
    ],
    "companions":[
        "KNIGHT",
        "SERVANT",
        "STEED"
    ],
    "locations": [
        "CASTLE",
        "VILLAGE",
        "CITY"
    ]
}

这里的实际问题只是 SET 子句的最后一行和 WITH 子句之间不必要的 ,。摆脱它,你就摆脱了语法错误。

也就是说,我强烈建议将每个 UNWIND 与作用于展开值的子句分组,然后 resetting the cardinality 在执行下一个 UNWIND 和处理之前返回到单个行。像这样:

//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity

WITH c, class

UNWIND class.items AS item
MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

WITH distinct c, class
UNWIND class.companions AS companion
MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

WITH distinct c, class
UNWIND class.locations AS location
MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)