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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
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 MATCH
es 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 MATCH
es 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 MATCH
es 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
Watch streaming output¶
tail -f entities-resolved.ndjson | jq
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.
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.
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.