Name Matching
Find a person or a company whose name is written differently from the way you typed it, then score how well each candidate matches and see why.
Name matching is two separate things, and you can use either alone:
- Retrieval widens the search so a name spelled another way is found at all. Turn it on with
name_matching. - Scoring orders what was found by how well the names actually match, and attaches the evidence. Turn it on with
name_score.
Marking a field
A field holds names when its schema says name_matching: true. The option needs a string or string[] field, and cannot be combined with stem or stem_dictionary: names are not stemmed, so the field's terms are its words as written.
{
"name": "entities",
"fields": [
{"name": "legal_name", "type": "string", "name_matching": true},
{"name": "country", "type": "string", "facet": true}
]
}Marking the field adds two ways for a word to be found besides itself, each costing what a typo costs, so scoring and highlighting treat them the way they treat typo matches:
| Channel | Finds | Cost |
|---|---|---|
fold | Passport spellings of Nordic and German letters: "Oedegaard" and "Ødegård", "Mueller" and "Müller" | 1 typo |
double_metaphone | Words that share a sound, by Double Metaphone's primary or alternate code | 2 typos |
daitch_mokotoff | Words that share a sound, by Daitch-Mokotoff Soundex, every branch | 2 typos |
The keys are computed from the field's terms when a segment is written and recomputed when segments merge, so they are the same whatever built the segment: a collection built offline matches exactly as one indexed on the server does.
Searching
name_matching decides when the extra channels are used, and takes a value per query_by field:
| Value | Behavior |
|---|---|
off | Words are found only as written, plus the usual typo matching. The default. |
fallback | The channels are used only when the words as written find too little, like typo escalation. |
always | Every channel is expanded up front, whatever the words as written find. |
A value other than off only applies to fields the schema marks name_matching: true.
Scoring
name_score=true takes the search's first name_score_candidates hits (default 100, maximum 1000), scores each one's name against the query between 0 and 1, orders them by that score, and takes the page from them. Retrieval is for recall; the score is what you make a decision on.
It needs a name in q and a query_by field marked name_matching: true, and does not work with group_by or a union.
Tell it what the query names with name_score_kind, person or organization. This is not a detail: a person's name of one part identifies nobody, so the listed name's remaining parts count in full and "Andersson" does not match "William Anderson". A company's one distinctive word does identify it, so "Gazprom" matches "Gazprom Neft". The names alone cannot tell which case you are in, so the client says.
curl -H "X-TYPESENSE-API-KEY: KEY" \
"http://localhost:8108/collections/entities/documents/search\
?q=Mohammed+Al+Sabah\
&query_by=legal_name\
&name_matching=always\
&name_score=true\
&name_score_kind=person"Each hit carries a name_score object: the best of its names in the searched name-matching fields, the pairs behind the score, and whatever stayed unpaired on each side.
{
"document": {"legal_name": "Muhamad al-Sabah", "country": "KW"},
"name_score": {
"field": "legal_name",
"name": "Muhamad al-Sabah",
"pairs": [
{"kind": "exact", "name": "al", "query": "al", "similarity": 1.0},
{"kind": "exact", "name": "sabah", "query": "sabah", "similarity": 1.0},
{"encoder": "double_metaphone", "kind": "phonetic",
"name": "muhamad", "query": "mohammed", "similarity": 0.62}
],
"score": 0.71,
"unmatched_name": [],
"unmatched_query": []
}
}Every pair has a kind, and phonetic pairs also name the encoder that produced them:
kind | Means |
|---|---|
exact | The two parts are the same word. |
fold | The same word by passport spelling. |
typo | An edit that keeps most of the word. Confusable letters and a doubled letter cost less; a letter added or dropped in front costs more, and three-letter words are never paired this way. |
phonetic | Only a shared sound, in words not too far apart in spelling. Weak evidence: it never decides a match on its own. |
initial | A one-letter part against a part starting with that letter. Weak too. |
How the score is reached
- Parts pair in any order, best pair first, so word order does not matter. A part may also pair with two or three neighbouring parts of the other name run together, which is how "alsabah" pairs with "al sabah".
- Parts weigh by how rare they are in the collection, so two names sharing only "Mohammed" score low. Particles (al, bin, von), honorifics (Dr, Sheikh), single letters and the words that make a company's name a company's (AB, Ltd, Group, Holding) weigh little.
- A query part left unpaired costs more than a listed part left unpaired. A listed name often carries middle names and aliases the query does not repeat.
The approach follows OpenSanctions' logic-v2 matcher (rigour, MIT; see THIRD-PARTY-NOTICES in the repository), on Torque's own terms. The weights are data, not constants in the code, so they can be fitted to real decisions.
Explaining a match
explain_matches=true adds to text_match_info.matches what each query word matched and how. It is independent of name matching and works on any field, which makes it the quickest way to see whether a hit came from the word as written, a typo, a fold, a phonetic key or a completion.
See also
- Search - typo tolerance, prefixes and the rest of the query surface.
- Collections - the field options a schema takes.
- Filtering - narrowing candidates by country, type or status before they are scored.