From this SO Question: http://stackoverflow.com/questions/42735026/neo4j-import-csv-dhcp-data-controlling-duplication/42735376#42735376[Neo4j import csv / DHCP data controlling duplication]

____
I have a csv from DHCP with _time, hostname, IP_addr

I would like to add any changed IPs as new relationships, but keep the old ip relationships with a status attribute inactive, also think I want to limt to the last 10.

I am not sure the easiest way to do this in cypher, or should I be in python for this complexity

maybe an always add (remove duplicates)/csv import

and a second query to deactivate any old ips (how do I query non current if i have time as an attribute of relationship)

and a third query to remove relationships that if more that 10 previous ips are hanging off it.
____

== Answer
Sounds like fun. Not sure if every host-ip combination appears only once in a csv or also at later times like an "still-here" update

=== Import Statement

    LOAD CSV FROM "url" AS row
    MERGE (h:Host {name:row.hostname})
    MERGE (ip:IP {name:row.IP_addr})
    MERGE (h)-[:IP]->(ip) ON CREATE SET rel.created = row._time, rel.status = 1
    // optional for pre-existing/previous rels 
    ON MATCH SET rel.status = 0
    SET rel.updated = row._time;

=== Cleanup statement

    MATCH (h:Host) WHERE size( (h)-[:IP]->() ) > 1
    MATCH (h)-[rel:IP]->(:IP)
    WITH h,rel ORDER BY rel.updated DESC
    WITH h, collect(rel) as rels
    // not necessary when the status is set above
    FOREACH (r in rels[1..9] | SET r.status=0)
    FOREACH (r IN rels[10..-1] | DELETE r)



=== When the status is set correctly in the load statement

    MATCH (h:Host)-[rel:IP {status:0}]->(:IP)
    WITH h,rel ORDER BY rel.updated DESC
    WITH h, collect(rel) as rels
    FOREACH (r IN rels[9..-1] | DELETE r)

