Skip to content

Entity Resolution

Full Recipe

Shared by: Ryan Wright

Learn how real-time entity resolution – the deduplication of similar data – can drastically help with creating a comprehensive view of your data.

See the recipe in action on Confluent, and on YouTube.

Entity Resolution Recipe
1

Download Recipe

Scenario

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.

Sample Data

Before running this Recipe, download the dataset.

Note

Download the sample data to the same directory where you will run Quine.

curl -L https://recipes.quine.io/public-record-addresses-2021 -o public-record-addresses-2021.ndjson

How it Works

Ingest Query

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), (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: 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.

Standing Queries

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.

First Standing Query

The first standing query incrementally MATCHes the pattern when an entity has :poBox and :postcode edges from itself to the respective nodes:

- 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

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).

outputs:
  resolved:
    type: CypherQuery
    query: >-
      MATCH (e), (canonical)
      WHERE id(e) = $that.data.entity
        AND id(canonical) = idFrom($that.data.poBox, $that.data.postcode)
      SET canonical.canonical = {poBox: $that.data.poBox, postcode: $that.data.postcode},
          canonical: Canonical
      CREATE (e)-[:resolved]->(canonical)

Second Standing Query

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: 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

When this standing query MATCHes the resolved pattern above, it passes down the id's of the record and the resolved canonical node to the standing query output.

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"

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.

Sample resolved 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"
    }
  }
}

Running the Recipe

Start Recipe, trigger streaming input

java -jar quine-1.8.2.jar -r entity-resolution.yaml

Recipe Running

Watch streaming output

tail -f entities-resolved.ndjson | jq

Streaming Output

Experimenting with the Standing Queries

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.

No canonical entity for record

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 Resolved Streamed Entity Resolution

Quick Queries

Here are the quick queries defined in this recipe.

Quick Query Node Type Description
Adjacent Nodes All Display the nodes that are adjacent to this node.
Refresh All Refresh the content stored in a node
Local Properties All Display the properties stored by the node
Property Subgraph Entity Renders the address parts of an entity node
Records Entity Renders the records for an entity node
Resolved Entities Entity Renders the entity nodes which resolve to the same canonical node
Canonical Entity Entity Renders the canonical node which the entity resolves to
A.K.A. Entity/Canonical Renders all the distinct addressee fields for the given entity or canonical node

Tip

Quick Queries are available by right clicking on a node. The quick queries defined in this recipe are listed below.

Summary

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.