0
votes

I want to insert a multi level list into neo4j 4.1.1 via cypher query language.

The data I want to insert is a list of reports returned in json. One of the properties is the models behind the report. I would like to make a node for each report in my list + a node for every model on the given report and add a relationship between the report and the model (given on the report). This will be a daily data refresh, so I would like to create new report nodes if they do not exist in the neo4j database, if the report already exist I want to overwrite the data in case we have new information.

Example data:

[
    {
        "name": "Report 1",
        "description": "Report Description",
        "createdBy": "TRAINING1",
        "id": "04CA095985D9A838E10000000A4E740E",
        "created": "2020-02-14T14:27:56.371Z",
        "changed": "2020-02-14T14:27:56.371Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "Model 1",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model 2",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model 3",
                "id": "id of the model",
                "isPlanning": true
            },
          
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    },
     {
        "name": "Report 2",
        "description": "Report Description",
        "createdBy": "TRAINING1",
        "id": "0653F758E5264C49E10000000A4E740E",
        "created": "2020-02-14T14:27:56.371Z",
        "changed": "2020-02-14T14:27:56.371Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
          
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    },
    {
        "name": "Report Name 3",
        "description": "Report description 3",
        "createdBy": "TRAINING1",
        "id": "0653F758E5264C49E10000000A4E740E",
        "created": "2020-02-14T14:27:55.010Z",
        "changed": "2020-02-14T14:27:55.010Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "description of the model",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "model description",
                "id": "this is an id",
                "isPlanning": true
            },
            {
                "description": "model description",
                "id": "this is an id",
                "isPlanning": true
            }
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    }
]

The graph I would like to become is the following: Example graph

The query I tried was the following:

UNWIND $propsArray as props
      MERGE (sac:sacreports { id: props.id })
        ON CREATE
          SET sac = {id: props.id, name: props.name }
          UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m = {id: model.id, description: model.description }
          ON MATCH SET m = {id: model.id, description: model.description }
        ON MATCH 
          SET sac = {id: props.id, name: props.name, description: props.description }
          UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m = {id: model.id, description: model.description }
          ON MATCH SET m = {id: model.id, description: model.description }
          
      RETURN sac

I get the following error: Neo4jError: WITH is required between MERGE and UNWIND (line 7, column 11 (offset: 181)).

Any suggestion on how to perform this operation?

Thanks in advance!

1

1 Answers

0
votes

I think that ON CREATE and ON MATCH only supports a single SET clause. And you never create a relationship between a report and a model in your query. And if you are merging by the id of the node, you don't want to then overwrite it in the ON CREATE statement. I would use the following query:

UNWIND $propsArray as props
      MERGE (sac:sacreports { id: props.id })
        ON CREATE SET sac += {name: props.name , description: props.description}
      WITH sac, props
      UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m += {description: model.description }
          MERGE (sac)-[:IS_USED_IN]->(m)