Multiple if/else using apoc.do.case

Hi !

I'm trying to combine the use of 2 apoc functions and I'm stuck with the use of simple/double quotes.

Here is my query :

CALL apoc.periodic.iterate('UNWIND $parameters as row
    WITH row WHERE row.RAF_ID =~ "[0-9]+" RETURN row',
    'CALL apoc.do.case([$row.Legal_Entity_Type CONTAINS "Special", "MATCH (s:Structure {name: "SPV"}), (cl:Customer {raf_id: $row.RAF_ID})
    MERGE (cl)-[:HAS_TYPE]->(s)",
    $row.Legal_Entity_Type CONTAINS "Trust", "MATCH (s:Structure {name: "Trust or foundation"}), (cl:Customer {raf_id: $row.RAF_ID})
    MERGE (cl)-[:HAS_TYPE]->(s)",
    $row.Legal_Entity_Type CONTAINS "Family", "MATCH (s:Structure {name: "Family office"}), (cl:Customer {raf_id: $row.RAF_ID})
    MERGE (cl)-[:HAS_TYPE]->(s)",
    $row.Legal_Entity_Type CONTAINS "charity", "MATCH (s:Structure {name: "Charity"}), (cl:Customer {raf_id: $row.RAF_ID})
    MERGE (cl)-[:HAS_TYPE]->(s)",
    $row.Legal_Entity_Type CONTAINS "EIG", "MATCH (s:Structure {name: "EIG"}), (cl:Customer {raf_id: $row.RAF_ID})
    MERGE (cl)-[:HAS_TYPE]->(s)"
    ], "", {row:row}) YIELD value',
    {batchSize:2000, params: {parameters:$parameters}})

Is it possible to use the condition in apoc.do.case with this kind of query to find out if a string contains a word and if yes, matching the proper node and create the relationship ?
Or am I going to use multiple apoc.periodic.iterate for each kind of word I want to match ?

Thanks

Usually you can just compose them,

you just need to pass in the information from the outer to the inner statement as parameters as you've done.

You can just iterate over the list within the inner statement using an unwind

MATCH (cl:Customer {raf_id: $row.RAF_ID})
UNWIND [[Special','SPV'],  ...] as pairs // can also be a parameter list
WITH row, cl, pairs[0] as type, pairs[1] as name
WITH * WHERE $row.Legal_Entity_Type CONTAINS type
MATCH (s:Structure {name: name})
MERGE (cl)-[:HAS_TYPE]->(s)

Depending on your domain you might have an easier time just adding a label instead of creating the structure category node.

1 Like

I never thought about using a list, that's a great idea !

I don't know when it's worth using another label than creating a relationship. Is it depending the number of nodes ?

PS : You just miss the row in the ligne WITH cl, pairs[0] as type, pairs[1] as name, row. It works perfectly though