Search code examples
neo4jcyphervsm

Neo4j: Create a list of items when contain another item


I want to create a new list (c2) out of intersection of Buy1 with an Item346 Buy1 : [Item 12, Item 23, Item 7, Item 562, Item 346, Item 85]

List I want to return (vsm representation) c2 : [0, 0, 0, 0, 1, 0]


Solution

  • If you really want 0's for when the values aren't equal, and 1's for when they are, you can use a list extraction using CASE to handle the output:

    WITH ['Item 12', "Item 23", "Item 7", "Item 562", "Item 346", "Item 85"] as buy1, "Item 346" as item
    RETURN [i in buy1 | CASE WHEN i = item THEN 1 ELSE 0 END] as output 
    

    EDIT: If your input is a list of items, and not just a single item, then we can use a list membership check (using IN) to decide when to output 1 or 0:

    WITH ['Item 12', "Item 23", "Item 7", "Item 562", "Item 346", "Item 85"] as buy1, ["Item 346", "Item 7"] as items
    RETURN [i in buy1 | CASE WHEN i IN items THEN 1 ELSE 0 END] as output