version:1title:Entity Resolution Examplecontributor:https://github.com/rrwrightsummary:Entity Resolutiondescription:Ingest address records from a public dataset and form each record into a subgraph built from its properties. The records are initially resolved according to the same use of the "addressee". Records are further resolved as their subgraphs overlap and a rule is applied with a standing query to resolve all entities with the same `poBox` and `postcode`.iconImage:🤷ingestStreams:-type:FileIngestpath:public-record-addresses-2021.ndjsonformat:type:CypherJsonquery:>-WITH $that.parts AS partsMATCH (record), (entity), (cityDistrict), (unit), (country), (state), (level), (suburb), (city), (road), (house), (houseNumber), (poBox), (category), (near), (stateDistrict), (staircase), (postcode)WHERE id(record) = idFrom($that)AND id(entity) = idFrom($that.addressee, parts)AND id(cityDistrict) = idFrom("cityDistrict", CASE WHEN parts.cityDistrict IS NULL THEN -1 ELSE parts.cityDistrict END)AND id(unit) = idFrom("unit", CASE WHEN parts.unit IS NULL THEN -1 ELSE parts.unit END)AND id(country) = idFrom("country", CASE WHEN parts.country IS NULL THEN -1 ELSE parts.country END)AND id(state) = idFrom("state", CASE WHEN parts.state IS NULL THEN -1 ELSE parts.state END)AND id(level) = idFrom("level", CASE WHEN parts.level IS NULL THEN -1 ELSE parts.level END)AND id(suburb) = idFrom("suburb", CASE WHEN parts.suburb IS NULL THEN -1 ELSE parts.suburb END)AND id(city) = idFrom("city", CASE WHEN parts.city IS NULL THEN -1 ELSE parts.city END)AND id(road) = idFrom("road", CASE WHEN parts.road IS NULL THEN -1 ELSE parts.road END)AND id(house) = idFrom("house", CASE WHEN parts.house IS NULL THEN -1 ELSE parts.house END)AND id(houseNumber) = idFrom("houseNumber", CASE WHEN parts.houseNumber IS NULL THEN -1 ELSE parts.houseNumber END)AND id(poBox) = idFrom("poBox", CASE WHEN parts.poBox IS NULL THEN -1 ELSE parts.poBox END)AND id(category) = idFrom("category", CASE WHEN parts.category IS NULL THEN -1 ELSE parts.category END)AND id(near) = idFrom("near", CASE WHEN parts.near IS NULL THEN -1 ELSE parts.near END)AND id(stateDistrict) = idFrom("stateDistrict", CASE WHEN parts.stateDistrict IS NULL THEN -1 ELSE parts.stateDistrict END)AND id(staircase) = idFrom("staircase", CASE WHEN parts.staircase IS NULL THEN -1 ELSE parts.staircase END)AND id(postcode) = idFrom("postcode", CASE WHEN parts.postcode IS NULL THEN -1 ELSE parts.postcode END)FOREACH (p IN CASE WHEN parts.cityDistrict IS NULL THEN [] ELSE [parts.cityDistrict] END | SET cityDistrict.cityDistrict = p CREATE (entity)-[:cityDistrict]->(cityDistrict) )FOREACH (p IN CASE WHEN parts.unit IS NULL THEN [] ELSE [parts.unit] END | SET unit.unit = p CREATE (entity)-[:unit]->(unit) )FOREACH (p IN CASE WHEN parts.country IS NULL THEN [] ELSE [parts.country] END | SET country.country = p CREATE (entity)-[:country]->(country) )FOREACH (p IN CASE WHEN parts.state IS NULL THEN [] ELSE [parts.state] END | SET state.state = p CREATE (entity)-[:state]->(state) )FOREACH (p IN CASE WHEN parts.level IS NULL THEN [] ELSE [parts.level] END | SET level.level = p CREATE (entity)-[:level]->(level) )FOREACH (p IN CASE WHEN parts.suburb IS NULL THEN [] ELSE [parts.suburb] END | SET suburb.suburb = p CREATE (entity)-[:suburb]->(suburb) )FOREACH (p IN CASE WHEN parts.city IS NULL THEN [] ELSE [parts.city] END | SET city.city = p CREATE (entity)-[:city]->(city) )FOREACH (p IN CASE WHEN parts.road IS NULL THEN [] ELSE [parts.road] END | SET road.road = p CREATE (entity)-[:road]->(road) )FOREACH (p IN CASE WHEN parts.house IS NULL THEN [] ELSE [parts.house] END | SET house.house = p CREATE (entity)-[:house]->(house) )FOREACH (p IN CASE WHEN parts.houseNumber IS NULL THEN [] ELSE [parts.houseNumber] END | SET houseNumber.houseNumber = p CREATE (entity)-[:houseNumber]->(houseNumber) )FOREACH (p IN CASE WHEN parts.poBox IS NULL THEN [] ELSE [parts.poBox] END | SET poBox.poBox = p CREATE (entity)-[:poBox]->(poBox) )FOREACH (p IN CASE WHEN parts.category IS NULL THEN [] ELSE [parts.category] END | SET category.category = p CREATE (entity)-[:category]->(category) )FOREACH (p IN CASE WHEN parts.near IS NULL THEN [] ELSE [parts.near] END | SET near.near = p CREATE (entity)-[:near]->(near) )FOREACH (p IN CASE WHEN parts.stateDistrict IS NULL THEN [] ELSE [parts.stateDistrict] END | SET stateDistrict.stateDistrict = p CREATE (entity)-[:stateDistrict]->(stateDistrict) )FOREACH (p IN CASE WHEN parts.staircase IS NULL THEN [] ELSE [parts.staircase] END | SET staircase.staircase = p CREATE (entity)-[:staircase]->(staircase) )FOREACH (p IN CASE WHEN parts.postcode IS NULL THEN [] ELSE [parts.postcode] END | SET postcode.postcode = p CREATE (entity)-[:postcode]->(postcode) )SET entity = parts,entity.addressee = $that.addressee,entity: Entity,record = $that,record: RecordCREATE (record)-[:record_for_entity]->(entity)standingQueries:-pattern:# This creates the `canoncial` record based on postcode and poBox and connects it.type:Cyphermode:MultipleValuesquery:>-MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc)RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcodeoutputs:resolved:type:CypherQueryquery:>-MATCH (e), (canonical)WHERE id(e) = $that.data.entityAND id(canonical) = idFrom($that.data.poBox, $that.data.postcode)SET canonical.canonical = {poBox: $that.data.poBox, postcode: $that.data.postcode},canonical: CanonicalCREATE (e)-[:resolved]->(canonical)-pattern:# This re-emits the original record with a field showing its resolution.type:Cyphermode:MultipleValuesquery:>-MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved)WHERE resolved.canonical IS NOT NULLRETURN id(record) AS record, id(resolved) AS resolvedoutputs:resolved-record:type:CypherQueryquery:>-MATCH (record)WHERE id(record) = $that.data.recordWITH properties(record) as props RETURN props {.*, resolved: $that.data.resolved} AS resolved_entityandThen:type:WriteToFilepath:"entities-resolved.ndjson"nodeAppearances:-predicate:propertyKeys:-partsknownValues:{}label:prefix:""key:idtype:Propertyicon:"📝"-predicate:propertyKeys:-addresseeknownValues:{}label:prefix:""key:addresseetype:Propertyicon:"🤷"-predicate:propertyKeys:-cityDistrictknownValues:{}label:prefix:"cityDistrict:"key:cityDistricttype:Propertyicon:"🏙️"-predicate:propertyKeys:-unitknownValues:{}label:prefix:"unit:"key:unittype:Propertyicon:"#"-predicate:propertyKeys:-countryknownValues:{}label:prefix:"country:"key:countrytype:Propertyicon:"🇺🇳"-predicate:propertyKeys:-stateknownValues:{}label:prefix:"state:"key:statetype:Propertyicon:"🇺🇸"-predicate:propertyKeys:-levelknownValues:{}label:prefix:"level:"key:leveltype:Propertyicon:"🎚️"-predicate:propertyKeys:-suburbknownValues:{}label:prefix:"suburb:"key:suburbtype:Propertyicon:"🏘️"-predicate:propertyKeys:-cityknownValues:{}label:prefix:"city:"key:citytype:Propertyicon:"🌃"-predicate:propertyKeys:-roadknownValues:{}label:prefix:"road:"key:roadtype:Propertyicon:"🛣️"-predicate:propertyKeys:-houseknownValues:{}label:prefix:"house:"key:housetype:Propertyicon:"🏡"-predicate:propertyKeys:-houseNumberknownValues:{}label:prefix:"houseNumber:"key:houseNumbertype:Propertyicon:"💯"-predicate:propertyKeys:-poBoxknownValues:{}label:prefix:"poBox:"key:poBoxtype:Propertyicon:"🔢"-predicate:propertyKeys:-categoryknownValues:{}label:prefix:"category:"key:categorytype:Propertyicon:"🐈"-predicate:propertyKeys:-nearknownValues:{}label:prefix:"near:"key:neartype:Propertyicon:"⤵️"-predicate:propertyKeys:-stateDistrictknownValues:{}label:prefix:"stateDistrict:"key:stateDistricttype:Propertyicon:"🌁"-predicate:propertyKeys:-staircaseknownValues:{}label:prefix:"staircase:"key:staircasetype:Propertyicon:"🪜"-predicate:propertyKeys:-postcodeknownValues:{}label:prefix:"postcode:"key:postcodetype:Propertyicon:"✉️"-predicate:propertyKeys:-canonicalknownValues:{}label:value:"CanonicalEntity"type:Constanticon:"🧑⚖️"quickQueries:-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Adjacent NodesquerySuffix:MATCH (n)--(m) RETURN DISTINCT mqueryLanguage:Cyphersort:Node-predicate:propertyKeys:[]knownValues:{}quickQuery:name:RefreshquerySuffix:RETURN nqueryLanguage:Cyphersort:Node-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Local PropertiesquerySuffix:RETURN id(n), properties(n)queryLanguage:Cyphersort:Text-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Property SubgraphqueryLanguage:Cyphersort:NodequerySuffix:MATCH (n)-->(m) WHERE m.parsed IS NULL AND m.canonical IS NULL RETURN m-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:RecordsquerySuffix:MATCH (n)<-[:record_for_entity]-(r) RETURN rqueryLanguage:Cyphersort:Node-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Resolved EntitiesquerySuffix:MATCH (n)-[:resolved]->(r)<-[:resolved]-(e) RETURN equeryLanguage:Cyphersort:NodeedgeLabel:Resolved-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Canonical EntityquerySuffix:MATCH (n)-[:resolved]->(r) RETURN rqueryLanguage:Cyphersort:Node-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)-[:resolved]->(c)<-[:resolved]-(o) RETURN DISTINCT replace(o.addressee, "\n", " ") AS AKAqueryLanguage:Cyphersort:Text-predicate:propertyKeys:[canonical]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)<-[:resolved]-(o) RETURN replace(o.addressee, "\n", " ") AS AKAqueryLanguage:Cyphersort:TextsampleQueries:-name:Recent nodequery:CALL recentNodes(1)-name:Show one recordquery:MATCH (a) WHERE id(a) = "00145c03-428c-3051-9d9c-c09c5f4eace4" RETURN a-name:Missing PO Boxquery:MATCH (n) WHERE strId(n) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" RETURN n-name:Create missing PO BOXquery:WITH "hand-created box 12345" as box MATCH (entity), (poBox) WHERE strId(entity) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" AND id(poBox) = idFrom("poBox", box) SET poBox.poBox = box CREATE (entity)-[:poBox]->(poBox) RETURN poBox
version:2title:Entity Resolution Examplecontributor:https://github.com/rrwrightsummary:Entity Resolutiondescription:Ingest address records from a public dataset and form each record into a subgraph built from its properties. The records are initially resolved according to the same use of the "addressee". Records are further resolved as their subgraphs overlap and a rule is applied with a standing query to resolve all entities with the same `poBox` and `postcode`.iconImage:🤷ingestStreams:-name:address-recordssource:type:Filepath:$in_fileformat:type:JsonLquery:>-WITH $that.parts AS partsMATCH (record), (entity), (cityDistrict), (unit), (country), (state), (level), (suburb), (city), (road), (house), (houseNumber), (poBox), (category), (near), (stateDistrict), (staircase), (postcode)WHERE id(record) = idFrom($that)AND id(entity) = idFrom($that.addressee, parts)AND id(cityDistrict) = idFrom("cityDistrict", CASE WHEN parts.cityDistrict IS NULL THEN -1 ELSE parts.cityDistrict END)AND id(unit) = idFrom("unit", CASE WHEN parts.unit IS NULL THEN -1 ELSE parts.unit END)AND id(country) = idFrom("country", CASE WHEN parts.country IS NULL THEN -1 ELSE parts.country END)AND id(state) = idFrom("state", CASE WHEN parts.state IS NULL THEN -1 ELSE parts.state END)AND id(level) = idFrom("level", CASE WHEN parts.level IS NULL THEN -1 ELSE parts.level END)AND id(suburb) = idFrom("suburb", CASE WHEN parts.suburb IS NULL THEN -1 ELSE parts.suburb END)AND id(city) = idFrom("city", CASE WHEN parts.city IS NULL THEN -1 ELSE parts.city END)AND id(road) = idFrom("road", CASE WHEN parts.road IS NULL THEN -1 ELSE parts.road END)AND id(house) = idFrom("house", CASE WHEN parts.house IS NULL THEN -1 ELSE parts.house END)AND id(houseNumber) = idFrom("houseNumber", CASE WHEN parts.houseNumber IS NULL THEN -1 ELSE parts.houseNumber END)AND id(poBox) = idFrom("poBox", CASE WHEN parts.poBox IS NULL THEN -1 ELSE parts.poBox END)AND id(category) = idFrom("category", CASE WHEN parts.category IS NULL THEN -1 ELSE parts.category END)AND id(near) = idFrom("near", CASE WHEN parts.near IS NULL THEN -1 ELSE parts.near END)AND id(stateDistrict) = idFrom("stateDistrict", CASE WHEN parts.stateDistrict IS NULL THEN -1 ELSE parts.stateDistrict END)AND id(staircase) = idFrom("staircase", CASE WHEN parts.staircase IS NULL THEN -1 ELSE parts.staircase END)AND id(postcode) = idFrom("postcode", CASE WHEN parts.postcode IS NULL THEN -1 ELSE parts.postcode END)FOREACH (p IN CASE WHEN parts.cityDistrict IS NULL THEN [] ELSE [parts.cityDistrict] END | SET cityDistrict.cityDistrict = p CREATE (entity)-[:cityDistrict]->(cityDistrict) )FOREACH (p IN CASE WHEN parts.unit IS NULL THEN [] ELSE [parts.unit] END | SET unit.unit = p CREATE (entity)-[:unit]->(unit) )FOREACH (p IN CASE WHEN parts.country IS NULL THEN [] ELSE [parts.country] END | SET country.country = p CREATE (entity)-[:country]->(country) )FOREACH (p IN CASE WHEN parts.state IS NULL THEN [] ELSE [parts.state] END | SET state.state = p CREATE (entity)-[:state]->(state) )FOREACH (p IN CASE WHEN parts.level IS NULL THEN [] ELSE [parts.level] END | SET level.level = p CREATE (entity)-[:level]->(level) )FOREACH (p IN CASE WHEN parts.suburb IS NULL THEN [] ELSE [parts.suburb] END | SET suburb.suburb = p CREATE (entity)-[:suburb]->(suburb) )FOREACH (p IN CASE WHEN parts.city IS NULL THEN [] ELSE [parts.city] END | SET city.city = p CREATE (entity)-[:city]->(city) )FOREACH (p IN CASE WHEN parts.road IS NULL THEN [] ELSE [parts.road] END | SET road.road = p CREATE (entity)-[:road]->(road) )FOREACH (p IN CASE WHEN parts.house IS NULL THEN [] ELSE [parts.house] END | SET house.house = p CREATE (entity)-[:house]->(house) )FOREACH (p IN CASE WHEN parts.houseNumber IS NULL THEN [] ELSE [parts.houseNumber] END | SET houseNumber.houseNumber = p CREATE (entity)-[:houseNumber]->(houseNumber) )FOREACH (p IN CASE WHEN parts.poBox IS NULL THEN [] ELSE [parts.poBox] END | SET poBox.poBox = p CREATE (entity)-[:poBox]->(poBox) )FOREACH (p IN CASE WHEN parts.category IS NULL THEN [] ELSE [parts.category] END | SET category.category = p CREATE (entity)-[:category]->(category) )FOREACH (p IN CASE WHEN parts.near IS NULL THEN [] ELSE [parts.near] END | SET near.near = p CREATE (entity)-[:near]->(near) )FOREACH (p IN CASE WHEN parts.stateDistrict IS NULL THEN [] ELSE [parts.stateDistrict] END | SET stateDistrict.stateDistrict = p CREATE (entity)-[:stateDistrict]->(stateDistrict) )FOREACH (p IN CASE WHEN parts.staircase IS NULL THEN [] ELSE [parts.staircase] END | SET staircase.staircase = p CREATE (entity)-[:staircase]->(staircase) )FOREACH (p IN CASE WHEN parts.postcode IS NULL THEN [] ELSE [parts.postcode] END | SET postcode.postcode = p CREATE (entity)-[:postcode]->(postcode) )SET entity = parts,entity.addressee = $that.addressee,entity: Entity,record = $that,record: RecordCREATE (record)-[:record_for_entity]->(entity)standingQueries:# This creates the `canonical` record based on postcode and poBox and connects it.-name:resolve-by-pobox-postcodepattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc)RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcodeoutputs:-name:resolvedresultEnrichment:query:>-MATCH (e), (canonical)WHERE id(e) = $that.data.entityAND id(canonical) = idFrom($that.data.poBox, $that.data.postcode)SET canonical.canonical = {poBox: $that.data.poBox, postcode: $that.data.postcode},canonical: CanonicalCREATE (e)-[:resolved]->(canonical)RETURN nullparameter:thatdestinations:-type:Drop# This re-emits the original record with a field showing its resolution.-name:emit-resolved-recordspattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved)WHERE resolved.canonical IS NOT NULLRETURN id(record) AS record, id(resolved) AS resolvedoutputs:-name:resolved-recordresultEnrichment:query:>-MATCH (record)WHERE id(record) = $that.data.recordWITH properties(record) as propsRETURN props {.*, resolved: $that.data.resolved} AS resolved_entityparameter:thatdestinations:-type:Filepath:"entities-resolved.ndjson"nodeAppearances:-predicate:propertyKeys:-partsknownValues:{}label:prefix:""key:idtype:Propertyicon:"📝"-predicate:propertyKeys:-addresseeknownValues:{}label:prefix:""key:addresseetype:Propertyicon:"🤷"-predicate:propertyKeys:-cityDistrictknownValues:{}label:prefix:"cityDistrict:"key:cityDistricttype:Propertyicon:"🏙️"-predicate:propertyKeys:-unitknownValues:{}label:prefix:"unit:"key:unittype:Propertyicon:"#"-predicate:propertyKeys:-countryknownValues:{}label:prefix:"country:"key:countrytype:Propertyicon:"🇺🇳"-predicate:propertyKeys:-stateknownValues:{}label:prefix:"state:"key:statetype:Propertyicon:"🇺🇸"-predicate:propertyKeys:-levelknownValues:{}label:prefix:"level:"key:leveltype:Propertyicon:"🎚️"-predicate:propertyKeys:-suburbknownValues:{}label:prefix:"suburb:"key:suburbtype:Propertyicon:"🏘️"-predicate:propertyKeys:-cityknownValues:{}label:prefix:"city:"key:citytype:Propertyicon:"🌃"-predicate:propertyKeys:-roadknownValues:{}label:prefix:"road:"key:roadtype:Propertyicon:"🛣️"-predicate:propertyKeys:-houseknownValues:{}label:prefix:"house:"key:housetype:Propertyicon:"🏡"-predicate:propertyKeys:-houseNumberknownValues:{}label:prefix:"houseNumber:"key:houseNumbertype:Propertyicon:"💯"-predicate:propertyKeys:-poBoxknownValues:{}label:prefix:"poBox:"key:poBoxtype:Propertyicon:"🔢"-predicate:propertyKeys:-categoryknownValues:{}label:prefix:"category:"key:categorytype:Propertyicon:"🐈"-predicate:propertyKeys:-nearknownValues:{}label:prefix:"near:"key:neartype:Propertyicon:"⤵️"-predicate:propertyKeys:-stateDistrictknownValues:{}label:prefix:"stateDistrict:"key:stateDistricttype:Propertyicon:"🌁"-predicate:propertyKeys:-staircaseknownValues:{}label:prefix:"staircase:"key:staircasetype:Propertyicon:"🪜"-predicate:propertyKeys:-postcodeknownValues:{}label:prefix:"postcode:"key:postcodetype:Propertyicon:"✉️"-predicate:propertyKeys:-canonicalknownValues:{}label:value:"CanonicalEntity"type:Constanticon:"🧑⚖️"quickQueries:-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Adjacent NodesquerySuffix:MATCH (n)--(m) RETURN DISTINCT msort:NODE-predicate:propertyKeys:[]knownValues:{}quickQuery:name:RefreshquerySuffix:RETURN nsort:NODE-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Local PropertiesquerySuffix:RETURN id(n), properties(n)sort:TEXT-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Property Subgraphsort:NODEquerySuffix:MATCH (n)-->(m) WHERE m.parsed IS NULL AND m.canonical IS NULL RETURN m-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:RecordsquerySuffix:MATCH (n)<-[:record_for_entity]-(r) RETURN rsort:NODE-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Resolved EntitiesquerySuffix:MATCH (n)-[:resolved]->(r)<-[:resolved]-(e) RETURN esort:NODEedgeLabel:Resolved-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Canonical EntityquerySuffix:MATCH (n)-[:resolved]->(r) RETURN rsort:NODE-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)-[:resolved]->(c)<-[:resolved]-(o) RETURN DISTINCT replace(o.addressee, "\n", " ") AS AKAsort:TEXT-predicate:propertyKeys:[canonical]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)<-[:resolved]-(o) RETURN replace(o.addressee, "\n", " ") AS AKAsort:TEXTsampleQueries:-name:Recent nodequery:CALL recentNodes(1)-name:Show one recordquery:MATCH (a) WHERE id(a) = "00145c03-428c-3051-9d9c-c09c5f4eace4" RETURN a-name:Missing PO Boxquery:MATCH (n) WHERE strId(n) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" RETURN n-name:Create missing PO BOXquery:WITH "hand-created box 12345" as box MATCH (entity), (poBox) WHERE strId(entity) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" AND id(poBox) = idFrom("poBox", box) SET poBox.poBox = box CREATE (entity)-[:poBox]->(poBox) RETURN poBox
version:2title:Entity Resolution Examplecontributor:https://github.com/rrwrightsummary:Entity Resolutiondescription:Ingest address records from a public dataset and form each record into a subgraph built from its properties. The records are initially resolved according to the same use of the "addressee". Records are further resolved as their subgraphs overlap and a rule is applied with a standing query to resolve all entities with the same `poBox` and `postcode`.iconImage:🤷ingestStreams:-name:address-recordssource:type:ServerSentEventurl:https://data.thatdot.com/recipe/public-record-addresses-2021.ndjsonformat:type:Jsonquery:>-WITH $that.parts AS partsMATCH (record), (entity), (cityDistrict), (unit), (country), (state), (level), (suburb), (city), (road), (house), (houseNumber), (poBox), (category), (near), (stateDistrict), (staircase), (postcode)WHERE id(record) = idFrom($that)AND id(entity) = idFrom($that.addressee, parts)AND id(cityDistrict) = idFrom("cityDistrict", CASE WHEN parts.cityDistrict IS NULL THEN -1 ELSE parts.cityDistrict END)AND id(unit) = idFrom("unit", CASE WHEN parts.unit IS NULL THEN -1 ELSE parts.unit END)AND id(country) = idFrom("country", CASE WHEN parts.country IS NULL THEN -1 ELSE parts.country END)AND id(state) = idFrom("state", CASE WHEN parts.state IS NULL THEN -1 ELSE parts.state END)AND id(level) = idFrom("level", CASE WHEN parts.level IS NULL THEN -1 ELSE parts.level END)AND id(suburb) = idFrom("suburb", CASE WHEN parts.suburb IS NULL THEN -1 ELSE parts.suburb END)AND id(city) = idFrom("city", CASE WHEN parts.city IS NULL THEN -1 ELSE parts.city END)AND id(road) = idFrom("road", CASE WHEN parts.road IS NULL THEN -1 ELSE parts.road END)AND id(house) = idFrom("house", CASE WHEN parts.house IS NULL THEN -1 ELSE parts.house END)AND id(houseNumber) = idFrom("houseNumber", CASE WHEN parts.houseNumber IS NULL THEN -1 ELSE parts.houseNumber END)AND id(poBox) = idFrom("poBox", CASE WHEN parts.poBox IS NULL THEN -1 ELSE parts.poBox END)AND id(category) = idFrom("category", CASE WHEN parts.category IS NULL THEN -1 ELSE parts.category END)AND id(near) = idFrom("near", CASE WHEN parts.near IS NULL THEN -1 ELSE parts.near END)AND id(stateDistrict) = idFrom("stateDistrict", CASE WHEN parts.stateDistrict IS NULL THEN -1 ELSE parts.stateDistrict END)AND id(staircase) = idFrom("staircase", CASE WHEN parts.staircase IS NULL THEN -1 ELSE parts.staircase END)AND id(postcode) = idFrom("postcode", CASE WHEN parts.postcode IS NULL THEN -1 ELSE parts.postcode END)FOREACH (p IN CASE WHEN parts.cityDistrict IS NULL THEN [] ELSE [parts.cityDistrict] END | SET cityDistrict.cityDistrict = p CREATE (entity)-[:cityDistrict]->(cityDistrict) )FOREACH (p IN CASE WHEN parts.unit IS NULL THEN [] ELSE [parts.unit] END | SET unit.unit = p CREATE (entity)-[:unit]->(unit) )FOREACH (p IN CASE WHEN parts.country IS NULL THEN [] ELSE [parts.country] END | SET country.country = p CREATE (entity)-[:country]->(country) )FOREACH (p IN CASE WHEN parts.state IS NULL THEN [] ELSE [parts.state] END | SET state.state = p CREATE (entity)-[:state]->(state) )FOREACH (p IN CASE WHEN parts.level IS NULL THEN [] ELSE [parts.level] END | SET level.level = p CREATE (entity)-[:level]->(level) )FOREACH (p IN CASE WHEN parts.suburb IS NULL THEN [] ELSE [parts.suburb] END | SET suburb.suburb = p CREATE (entity)-[:suburb]->(suburb) )FOREACH (p IN CASE WHEN parts.city IS NULL THEN [] ELSE [parts.city] END | SET city.city = p CREATE (entity)-[:city]->(city) )FOREACH (p IN CASE WHEN parts.road IS NULL THEN [] ELSE [parts.road] END | SET road.road = p CREATE (entity)-[:road]->(road) )FOREACH (p IN CASE WHEN parts.house IS NULL THEN [] ELSE [parts.house] END | SET house.house = p CREATE (entity)-[:house]->(house) )FOREACH (p IN CASE WHEN parts.houseNumber IS NULL THEN [] ELSE [parts.houseNumber] END | SET houseNumber.houseNumber = p CREATE (entity)-[:houseNumber]->(houseNumber) )FOREACH (p IN CASE WHEN parts.poBox IS NULL THEN [] ELSE [parts.poBox] END | SET poBox.poBox = p CREATE (entity)-[:poBox]->(poBox) )FOREACH (p IN CASE WHEN parts.category IS NULL THEN [] ELSE [parts.category] END | SET category.category = p CREATE (entity)-[:category]->(category) )FOREACH (p IN CASE WHEN parts.near IS NULL THEN [] ELSE [parts.near] END | SET near.near = p CREATE (entity)-[:near]->(near) )FOREACH (p IN CASE WHEN parts.stateDistrict IS NULL THEN [] ELSE [parts.stateDistrict] END | SET stateDistrict.stateDistrict = p CREATE (entity)-[:stateDistrict]->(stateDistrict) )FOREACH (p IN CASE WHEN parts.staircase IS NULL THEN [] ELSE [parts.staircase] END | SET staircase.staircase = p CREATE (entity)-[:staircase]->(staircase) )FOREACH (p IN CASE WHEN parts.postcode IS NULL THEN [] ELSE [parts.postcode] END | SET postcode.postcode = p CREATE (entity)-[:postcode]->(postcode) )SET entity = parts,entity.addressee = $that.addressee,entity: Entity,record = $that,record: RecordCREATE (record)-[:record_for_entity]->(entity)standingQueries:# This creates the `canonical` record based on postcode and poBox and connects it.-name:resolve-by-pobox-postcodepattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc)RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcodeoutputs:-name:resolvedresultEnrichment:query:>-MATCH (e), (canonical)WHERE id(e) = $that.data.entityAND id(canonical) = idFrom($that.data.poBox, $that.data.postcode)SET canonical.canonical = {poBox: $that.data.poBox, postcode: $that.data.postcode},canonical: CanonicalCREATE (e)-[:resolved]->(canonical)RETURN nullparameter:thatdestinations:-type:Drop# This re-emits the original record with a field showing its resolution.-name:emit-resolved-recordspattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved)WHERE resolved.canonical IS NOT NULLRETURN id(record) AS record, id(resolved) AS resolvedoutputs:-name:resolved-recordresultEnrichment:query:>-MATCH (record)WHERE id(record) = $that.data.recordWITH properties(record) as propsRETURN props {.*, resolved: $that.data.resolved} AS resolved_entityparameter:thatdestinations:-type:Filepath:"entities-resolved.ndjson"nodeAppearances:-predicate:propertyKeys:-partsknownValues:{}label:prefix:""key:idtype:Propertyicon:"📝"-predicate:propertyKeys:-addresseeknownValues:{}label:prefix:""key:addresseetype:Propertyicon:"🤷"-predicate:propertyKeys:-cityDistrictknownValues:{}label:prefix:"cityDistrict:"key:cityDistricttype:Propertyicon:"🏙️"-predicate:propertyKeys:-unitknownValues:{}label:prefix:"unit:"key:unittype:Propertyicon:"#"-predicate:propertyKeys:-countryknownValues:{}label:prefix:"country:"key:countrytype:Propertyicon:"🇺🇳"-predicate:propertyKeys:-stateknownValues:{}label:prefix:"state:"key:statetype:Propertyicon:"🇺🇸"-predicate:propertyKeys:-levelknownValues:{}label:prefix:"level:"key:leveltype:Propertyicon:"🎚️"-predicate:propertyKeys:-suburbknownValues:{}label:prefix:"suburb:"key:suburbtype:Propertyicon:"🏘️"-predicate:propertyKeys:-cityknownValues:{}label:prefix:"city:"key:citytype:Propertyicon:"🌃"-predicate:propertyKeys:-roadknownValues:{}label:prefix:"road:"key:roadtype:Propertyicon:"🛣️"-predicate:propertyKeys:-houseknownValues:{}label:prefix:"house:"key:housetype:Propertyicon:"🏡"-predicate:propertyKeys:-houseNumberknownValues:{}label:prefix:"houseNumber:"key:houseNumbertype:Propertyicon:"💯"-predicate:propertyKeys:-poBoxknownValues:{}label:prefix:"poBox:"key:poBoxtype:Propertyicon:"🔢"-predicate:propertyKeys:-categoryknownValues:{}label:prefix:"category:"key:categorytype:Propertyicon:"🐈"-predicate:propertyKeys:-nearknownValues:{}label:prefix:"near:"key:neartype:Propertyicon:"⤵️"-predicate:propertyKeys:-stateDistrictknownValues:{}label:prefix:"stateDistrict:"key:stateDistricttype:Propertyicon:"🌁"-predicate:propertyKeys:-staircaseknownValues:{}label:prefix:"staircase:"key:staircasetype:Propertyicon:"🪜"-predicate:propertyKeys:-postcodeknownValues:{}label:prefix:"postcode:"key:postcodetype:Propertyicon:"✉️"-predicate:propertyKeys:-canonicalknownValues:{}label:value:"CanonicalEntity"type:Constanticon:"🧑⚖️"quickQueries:-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Adjacent NodesquerySuffix:MATCH (n)--(m) RETURN DISTINCT msort:NODE-predicate:propertyKeys:[]knownValues:{}quickQuery:name:RefreshquerySuffix:RETURN nsort:NODE-predicate:propertyKeys:[]knownValues:{}quickQuery:name:Local PropertiesquerySuffix:RETURN id(n), properties(n)sort:TEXT-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Property Subgraphsort:NODEquerySuffix:MATCH (n)-->(m) WHERE m.parsed IS NULL AND m.canonical IS NULL RETURN m-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:RecordsquerySuffix:MATCH (n)<-[:record_for_entity]-(r) RETURN rsort:NODE-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Resolved EntitiesquerySuffix:MATCH (n)-[:resolved]->(r)<-[:resolved]-(e) RETURN esort:NODEedgeLabel:Resolved-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:Canonical EntityquerySuffix:MATCH (n)-[:resolved]->(r) RETURN rsort:NODE-predicate:propertyKeys:[addressee]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)-[:resolved]->(c)<-[:resolved]-(o) RETURN DISTINCT replace(o.addressee, "\n", " ") AS AKAsort:TEXT-predicate:propertyKeys:[canonical]knownValues:{}quickQuery:name:A.K.A.querySuffix:MATCH (n)<-[:resolved]-(o) RETURN replace(o.addressee, "\n", " ") AS AKAsort:TEXTsampleQueries:-name:Recent nodequery:CALL recentNodes(1)-name:Show one recordquery:MATCH (a) WHERE id(a) = "00145c03-428c-3051-9d9c-c09c5f4eace4" RETURN a-name:Missing PO Boxquery:MATCH (n) WHERE strId(n) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" RETURN n-name:Create missing PO BOXquery:WITH "hand-created box 12345" as box MATCH (entity), (poBox) WHERE strId(entity) = "c2e78a44-05de-3fbf-98d1-c5bdad2790a0" AND id(poBox) = idFrom("poBox", box) SET poBox.poBox = box CREATE (entity)-[:poBox]->(poBox) RETURN poBox
We will resolve entities fast enough to do it live in a data pipeline. Entities will always be resolved for downstream. We will stream in sample data from public address information, and add a resolved field to each data record. This field will answer, "How does this resolve? How does this one address as written, resolve to one physical place?" This is the entity resolution problem for addresses, which this recipe will demo.
Here is the first record from the dataset to serve as an example:
{"original":"America First Credit U\nPo Box 9199\nOgden, UT 84409-0000","addressee":"america first credit u","parts":{"house":"america first credit u","poBox":"po box 9199","city":"ogden","state":"ut","postcode":"84409"}}
Note
While this recipe's ingest query does ingest data from a file, this could just as easily be switched out for a Kafka data source, or any other streaming source of data.
For each record in the dataset, this recipe's ingest query manifests that data into a record node on the graph, along with potentially manifesting data into an entity node (many records can point to one entity if their addressee and parts are the same). Along with these two nodes, data is also manifested into several other nodes, representing the different parts of an entity's address.
The ingest query also creates relationships between the record and its entity, along with relationships between an entity and its address parts (poBox, postcode, cityDistrict, road, country, etc).
Note
The standing queries discussed later on incrementally MATCH on the emergence of :poBox and :postcode edges from an entity node in the graph. These edges manifest from the ingest stream, triggering the standing queries as data is ingested into the graph, the instant these edges manifest.
{"type":"FileIngest","path":"public-record-addresses-2021.ndjson","format":{"type":"CypherJson","query":"WITH $that.parts AS parts MATCH (record), (entity), ... SET entity = parts, entity.addressee = $that.addressee, entity: Entity, record = $that, record: Record CREATE (record)-[:record_for_entity]->(entity)"}}
{"name":"address-records","source":{"type":"File","path":"$in_file","format":{"type":"JsonL"}},"query":"WITH $that.parts AS parts MATCH (record), (entity), ... SET entity = parts, entity.addressee = $that.addressee, entity: Entity, record = $that, record: Record CREATE (record)-[:record_for_entity]->(entity)"}
Tip
This recipe's ingest query uses Cypher's FOREACH like a poor man's IF statement. You'll see it used to conditionally manifest a node property and set an edge to that node.
Not every record in this dataset has the same set of parts, which is why we want some conditional cypher logic.
This recipe uses two standing queries to do the work of entity resolution based on an entity's poBox and postcode, adding the resolved property to each record which references the resolved entity, and emitting these resolved records downstream.
The first standing query incrementally MATCHes the pattern when an entity has :poBox and :postcode edges from itself to the respective nodes:
-pattern:type:Cyphermode:MultipleValuesquery:>-MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc)RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcode
POST /api/v1/query/standing/resolve-by-pobox-postcode
{"pattern":{"type":"Cypher","mode":"MultipleValues","query":"MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc) RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcode"}}
standingQueries:-name:resolve-by-pobox-postcodepattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc)RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcode
POST /api/v2/graph/{graphName}/standingQueries
{"name":"resolve-by-pobox-postcode","pattern":{"type":"Cypher","mode":"MULTIPLE_VALUES","query":"MATCH (pb)<-[:poBox]-(e)-[:postcode]->(pc) RETURN id(e) AS entity, pb.poBox AS poBox, pc.postcode AS postcode"}}
When that standing query finds this pattern, it passes down the entity's id, along with the poBox and postcode to the standing query output, which creates a canonical node for each entity, and a relationship to that node (via the :resolved edge).
The second standing query incrementally MATCHes the pattern involving the :resolved edge that is created when the first standing query matches the poBox and postcode pattern, specifically the moment an entity with a record is resolved to a canonical node.
-pattern:type:Cyphermode:MultipleValuesquery:>-MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved)WHERE resolved.canonical IS NOT NULLRETURN id(record) AS record, id(resolved) AS resolved
POST /api/v1/query/standing/emit-resolved-records
{"pattern":{"type":"Cypher","mode":"MultipleValues","query":"MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved) WHERE resolved.canonical IS NOT NULL RETURN id(record) AS record, id(resolved) AS resolved"}}
standingQueries:-name:emit-resolved-recordspattern:type:Cyphermode:MULTIPLE_VALUESquery:>-MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved)WHERE resolved.canonical IS NOT NULLRETURN id(record) AS record, id(resolved) AS resolved
POST /api/v2/graph/{graphName}/standingQueries
{"name":"emit-resolved-records","pattern":{"type":"Cypher","mode":"MULTIPLE_VALUES","query":"MATCH (record)-[:record_for_entity]->(entity)-[:resolved]->(resolved) WHERE resolved.canonical IS NOT NULL RETURN id(record) AS record, id(resolved) AS resolved"}}
When this standing query MATCHes the resolved pattern above, it passes down the id's of the record and the resolvedcanonical node to the standing query output.
outputs:resolved-record:type:CypherQueryquery:>-MATCH (record)WHERE id(record) = $that.data.recordWITH properties(record) as propsRETURN props {.*, resolved: $that.data.resolved} AS resolved_entityandThen:type:WriteToFilepath:"entities-resolved.ndjson"
{"outputs":{"resolved-record":{"type":"CypherQuery","query":"MATCH (record) WHERE id(record) = $that.data.record WITH properties(record) as props RETURN props {.*, resolved: $that.data.resolved} AS resolved_entity","andThen":{"type":"WriteToFile","path":"entities-resolved.ndjson"}}}}
outputs:-name:resolved-recordresultEnrichment:query:>-MATCH (record)WHERE id(record) = $that.data.recordWITH properties(record) as propsRETURN props {.*, resolved: $that.data.resolved} AS resolved_entityparameter:thatdestinations:-type:Filepath:"entities-resolved.ndjson"
{"outputs":[{"name":"resolved-record","resultEnrichment":{"query":"MATCH (record) WHERE id(record) = $that.data.record WITH properties(record) as props RETURN props {.*, resolved: $that.data.resolved} AS resolved_entity","parameter":"that"},"destinations":[{"type":"File","path":"entities-resolved.ndjson"}]}]}
This standing query output emits to the entities-resolved.ndjson each record, along with their added resolved field, which contains the id of the canonical node which the entity resolves to.
Tip
Note the use of the "all-properties selector" .* which is used to project all key-value pairs from the record.
{"meta":{"isPositiveMatch":true},"data":{"resolved_entity":{"addressee":"pital one\npo \n ca","original":"Capital One\nP.O. Box 60024\nCity Of Industry, CA 91716-0024","parts":{"city":"city of industry","house":"capital one po","poBox":"po box 60024","postcode":"91716","state":"ca"},"resolved":"bcc26a64-20da-3816-bc4c-fe1ea6d1e1f8"}}}
This recipe includes several sample queries and quick queries to aid in exploring the graph, along with experimenting with how the standing queries incrementally match for emergent patterns in the graph, and how they continously respond to these patterns, emitting results downstream.
Open up the Quine web server running at http://127.0.0.1:8080/, and then click on the empty Query input field to see several sample queries that we will be using.
Sample Query
Description
Recent node
Renders the most recently modified/queried node
Show one record
Renders a specific record which has a postcode, but not a poBox
Missing PO Box
Renders the entity for the specific record with no poBox
Create missing PO BOX
Creates a poBox node, and creates the :poBox edge from the entity
We will use several of these sample queries, along with several quick queries, to explore the graph, and see the effect of the continuously running Standing Queries.
First, use the second sample query to render a specific record node. This record has a postcode property, but it does not have a poBox property. This means that this record's entity does not resolve.
We can verify this by using the third sample query to render the entity node for this specific record, using that entity node's Property Subgraph quick query to show a missing poBox, and using that node's Canonical Entity quick query, which will correctly NOT render anything, since without a poBox, the entity can't resolve to a canonical entity.
We can trigger the resolution of the "edcboardwalk realtyan" entity by creating a :poBox edge from this node. Trigger the fourth Create missing PO BOX sample query, and that edge will be created, and its node rendered on the graph.
Since this fulfills the standing query for entity resolution for the "edcboardwalk realtyan" entity, the standing query will resolve this entity to a Canonical Entity, which will be immediately streamed out to the output file, and we can view it by issuing that Canonical Entity quick query again.
Entity resolution no longer needs to happen at the end of your stream processing pipeline. The value of real-time analysis on entity resolved data can now be unlocked by using Quine today.