# Search-as-you-type for 54 Million Names: PostgreSQL + Django... with Tobias McNulty, Gerald Carlton

Source: https://djangotv.com/videos/djangocon-us/2026/search-as-you-type-for-54-million-names-postgresql-django-with-tobias-mcnulty-gerald-carlton/
Date: 2026-10-26
Speakers: Tobias McNulty, Speaker 2, Speaker 3, Speaker 4, Speaker 5, Speaker 6, Speaker 7, Speaker 8
Text: lightly edited for readability (verbatim on the page)

## Transcript

**Tobias McNulty:** [00:15] Thanks, everyone. We're really excited to be here today in Chicago again this year. This is a semi-interactive talk, if you would like to follow along. There's a Remo Notebook, a Django project, and a management command that you could run to generate some fake data at the URL or the QR code on the screen here. I'll leave that up there for just a minute. I'm Tobias. I'm joined today by my co-presenter, Gerald Carlton. We're both employee owners at Cactus Group, which is a web development company based in North Carolina. We are proud sponsors of DjangoCon and the Django Software Foundation. And yes.

[01:02] And we had a lot of help with this talk from our colleagues Simon and Colin, who helped with the initial implementation for the customer and these slides and the demo project as well. But before we dive into this project, we want to take a step back and talk about criminal record expungement. And for this walkthrough, I'll hand it over to Gerald.

**Speaker 2:** [01:29] Thanks, Tobias. First off, I want to say this is my first time being at DjangoCon. So far, it's been great. This is also my first time giving a talk, so double. But I keep hearing that this is the place to do a talk if it's your first time doing a talk. So I'm just excited to be here. Before we jump in, I want to give a bit of context. This talk was inspired by some work that we did for one of our clients who operates in the legal aid space. They came to us with an issue that we were able to help them solve. And what I'll walk you through is what their specific workflow was like.

[02:15] And where it became a hurdle for them in their day-to-day work. Criminal record expungement. So this is an area of legal aid where nonprofit organizations, public defenders, and private attorneys help members of the community to clear eligible charges from their record, such as dismissed cases or cases where they were found not guilty. One thing that some people may not realize is that a not-guilty or dismissed charge will still show on your record. So for someone trying to apply for housing or a job, an expungement can be life-changing.

[03:04] Under the hood, the process of preparing these petitions for expungements is painfully manual. Attorneys and paralegals must track down potentially hundreds of sub-records spanning decades, review every single charge, then manually enter everything into a strictly formatted court form, which could take a while. All right, so here's an overview of what that process looks like. Everything starts with the intake. A client will walk into an office looking for expungement. Staff members will then collect that person's name, every name that they've gone by, their aliases, dates of birth, and all the counties that they've lived in as well.

[03:51] So nothing gets missed in the record search that follows. Once all the data is collected, it's time to do a search. This will be what we focus on today. Staff members then have to search court records and law enforcement databases for every record that is tied to that particular client. And this is where things can get tricky, because names are sometimes typed or written inconsistently. There could be misspellings, nicknames, or abbreviations, such as someone who goes by Matt instead of Matthew. So a simple exact search will miss those records that exist in the database.

[04:39] Once every record is found for a client, each charge gets checked against a state statute to see if it qualifies for expungement. And then from there, every eligible charge is transcribed into a strictly formatted court petition by hand, which is another manual, error-prone process that could take time. Finally, the petition is completed, it's filed with the court, and if it's granted, those records are cleared. But getting here can take weeks of manual work for a single client. So when we first took on this project, we assumed that the main challenges would be things like

[05:29] mapping eligibility, applying those North Carolina statutes, and cleanly inserting them into standardized court forms. But that changed when we did a dedicated discovery workshop with the client. During the workshop, we realized that finding their proper records quickly was the true bottleneck preventing eligible community members from getting the help that they needed. Thorough record checks are critical because conviction expungements in North Carolina are a one-shot opportunity. So once a court grants a conviction expungement, the client can't use that statute again.

[06:16] So this means any omitted convictions will stay on their record if they're not caught in that first filing. Running a comprehensive statewide search is essential to make sure that none of those eligible charges are left off before the initial filing. All right, so here's the core problem. The state transitioned away from its legacy system to a more modern web portal. So you might think more modern means faster, better. That's not always the case. And this upgraded system severely restricted

[07:01] how staff members could search for records. It really slowed down the process compared with what they were using before. So we have a video here, and I'm not going to talk while it plays because I kind of want you guys to be in their shoes, to see how painful it can be to do this for multiple clients, just to get a taste of what it's like to spend this much time trying to find all the records. And this would be searching for—you can imagine a person comes to an office and gives their names, and now the staff member is trying to search for all the records that might be tied to this particular name.

[08:13] Hey. As you can see, that took quite a while. A simple search took 27 seconds, and it returned 200 cases. There are other instances where searches might have taken maybe 10 to 15 seconds, but not all searches were this long. It kind of just depends on what name is being searched. In our discovery, we found that the time it took for staff members to pull these records for one client grew from three hours up to 10 hours with the new system.

[09:00] So you can see how this was a big pain point for them. To make matters worse, while the system does claim to offer fuzzy search, the underlying logic was poorly implemented, where it can balloon a tight six-item return to over 200 useless matches, many of which were false positives, rather than giving the attorneys the results that they really need. So why is finding everyone so hard? In our case, we're dealing with a database of over 54 million North Carolina court records, and searching by name in a database is not easy.

[09:48] We're not just looking for a single record; we're looking for every record that belongs to that particular person. Secondly, think about the lifespan of a criminal record. You can imagine how frequent typos could be. A name could get typed in on the side of the road by an arresting officer. It gets transcribed from a handwritten form by a rushed court clerk, or it's provided by a client who uses nicknames, hyphenated names, or spells their name differently across different legal documents. And on top of that, collisions are everywhere. Thousands of people share the same first and last name. Date of birth is one of those fields that could disambiguate two people, but sometimes even that's missing in court data.

[10:36] So we realized that if we wanted to build a tool that would truly serve legal aid organizations, public defenders, and private attorneys, to solve that multimillion fuzzy-search record problem, we wanted to use tools of our choice, which were Django and PostgreSQL. And to talk more about that, I'll pass it over to you, Tobias.

**Tobias McNulty:** [10:59] Thanks, Joe. As evidenced in the demo that Gerald gave, and just in general, I think there are a lot of assumptions that we can make about human names. There's an article linked here which was helpful for us to set the stage, and I recommend reading it through when you have a moment. I won't go through all of them here, but I thought it would be fun to highlight just a few of them. One is that people have exactly one canonical name, when in reality, they may have different aliases

[11:48] or nicknames or spellings that they use regularly on legal documents, depending on the day or the week, and they'll alternate back and forth between them. Another assumption is that people's names don't change, when in fact, over a person's lifetime, through marriage or divorce or simply deciding to change one's name, a single person might not have the same name in all of the records in the database. Another example is that people have different first and last names, or perhaps—and this one may hit close to home for the Django user model—that they have exactly one first name and last name, and that's it.

[12:36] And, you know, because in different cultures and countries, even here in the US, names often don't fit into those neat first-name and last-name boxes. Another assumption we might make is that names are written in ASCII. For better or worse, we did not have to deal with this because in every single one of the fifty-four million records in the North Carolina state database, there's not a single non-ASCII character. It's true. Lastly, this was another one that hit close to home for our project:

[13:23] You know, two different people entering the same name will enter it the same way into a terminal or web form, when in fact, as Gerald talked about, the frequency of typos—or just hearing one name or spelling in their head and typing that down—means the spelling could be completely different from the person's name. And we found ample evidence of that in our database. Of course, we're not going to demo the actual production data of 54 million people here, but we did take time to put together a sample dataset that roughly has the same shape as the database that we are working with.

[14:09] For this, we found a dataset on Kaggle that had 1 million last names and 600,000 first names. We randomly weighted those according to the frequency of those names in the United States, combined them randomly, and assigned a random birth date. And each name, like Gerald talked about, is not in a nice denormalized database with a person table and then a court record table. It's just one big table with all the names, which might be different in different places. So each name might appear anywhere from one to eighty times in the sample database, with some typos injected just to make this more interesting.

[14:56] And so this is the data model in the sample project here, which again loosely follows the model of the data that we received from the state. There's a first name, last name, middle name, and date of birth. Just for the demo project, we added a person ID to that, which we didn't have, or at least we didn't have reliably in our data. There actually was one, but it wasn't always consistent, so we couldn't rely on it for tying different records together. So let's dive into what that looks like. And like I said, this is a...

[15:42] These slides are a Marimo notebook. We got to learn how that worked for this project. So there we go. So yeah, we can dive into that here. This is running on my laptop. We're in a notebook connected to this sample dataset that we generated. Let me see if I can make that a little bit bigger. Is that semi-readable for folks? Yes? Okay, thanks. So we have our court record model, and we're gonna filter for

[16:29] You know, exact match, case insensitive, on Sonia Mercias. And we get back 14 rows for that search. You can see they all have the same person ID and the same date of birth. So these are all corresponding to the same person. If we add the date of birth, we can see again that we get exactly 14 rows back. However, if we then search by that person ID field that we added, we can see that we missed a few rows in the initial search. We now get back 18 rows from that, and you can see some of the typos that are script-injected here.

[17:15] Sony, Sony, Sona. So there's 18 in total that we're trying to find. And a naive approach to this might be to inject some wildcards into the search or give the users the ability to put wildcards in there. You can almost get to it by searching for, you know, Sun and Reci, and then you get back 17 of the 18 rows that we were looking for in that way. But there's got to be a better way. So this talk could also be called “Why Is Postgres Awesome?”

[18:00] We relied heavily for this implementation on the fuzzystrmatch extension in Postgres. And in particular, we use the Daitch-Mokotoff algorithm, but there's a Soundex algorithm, which are roughly equivalent; they just do the same thing in different ways. So let's walk through an example of what those are. To start out with Soundex, this assigns an alphanumeric code to a word. You can see here, if we calculate the Soundex code of Sonya and Sania and Sania and so on—all of these different spellings—those all have the same code here: S500. So if you create an index on that and then match on that,

[18:46] you can find all of the rows in the table that correspond to that sound, according to this algorithm. And so if we update our earlier query to search on the Soundex of the first name and last name and date of birth, we get back the 18 rows that we were looking for without the person ID. And you can see all of the same typos—Sona and Sony, et cetera—in there. The issue with this phonetic algorithm, though, is that it does introduce a lot of false positives. We don't know the underlying implementation of the court system that we shared the video of, but we can assume it might implement something like this.

[19:33] And sort of stops there. So you can see if we search for Ricius on the last name, we also get back the last names Rakowski and Rosus and Rohus, and lots and lots of different false positives. And just to briefly demo the Daitch-Mokotoff algorithm, this works in pretty much the same way, except it assigns an array of sound codes to each word, and then you can use the overlap operator in Postgres to see if those codes overlap. According to this algorithm, they're determined to maybe have the same sound. So here

[20:18] we're searching for Michael and Mikael. Michael has both codes, 658000 and 648000, whereas Mikhail has just the code 658000, and those do overlap, so they're determined to have the same sound. So how do we deal with all the false positives that we saw, that Gerald mentioned and that we saw in the results here? One function that you can use to do that, also in the fuzzystrmatch module in Postgres, is the Levenshtein distance. And what this does is it calculates the number of character changes that are required to move from one

[21:03] word to another. So in this example, going from Michael to Mikhail, we need to substitute the C for a K, and then we need to remove the H. So that's determined to have a Levenshtein distance of two according to this algorithm. And then if we take one of the false positives that we saw from the earlier results, the Soundex code, you know, RISCS and ROSUS both have the same Soundex code of R220. But the number of character changes, or the edit distance between those two words, is three. So they do look more different, and they have a higher edit distance. So that's one

[21:49] mechanism you could use to tell how far apart these words are. When you're doing this, that's pretty inefficient to do on a large database. There's not really a way to create an index on that, because it does need to compare a certain word to every row in the table. But there is a slightly more efficient function that you can use for that, which is Levenshtein less equal. And instead of calculating the actual distance between two words, you can give it a stop point, at which point it won't calculate the actual distance; it will just return a value that's greater than the value that you passed in. So how do we use this in Django? Django does not have built-in support for

[22:35] these functions, but you can easily create them just by creating subclasses of the Func class and telling it what the output field is and what the function is it should be called in Postgres. To see what that looks like in practice, you would add an annotation with the Soundex code of the last name in this example. And then you could add a filter to find all rows where the Soundex code is equal to the Soundex code of the name Smith. And

[23:21] Django does have built-in support for trigram matching, which is another module folks may be familiar with. We tested this briefly, and it didn't end up working for us, but thought it would be worth mentioning nonetheless. The way this module works is it splits a word into slices of three characters. In Postgres's implementation, it will pad the first letter, first with two spaces and then one, and so forth. For example, Geraldine might be split into G E R, ERA, REL, et cetera. And you can use a similarity function in Postgres to calculate

[24:06] the similarity of those two words. So Geraldine and Gerald have a similarity score of 0.53. Morgan and Morrison have a score of 0.23. Two completely different words have a score of zero, and of course the same word will have a score of one. The trigram similarity and trigram distance classes are how you would use those in Django. We found that they were particularly sensitive to short names because when you have fewer characters to work with, there's only so much that can be determined between the two words using three-character slices. And there's a few different

[24:52] indexes that you can create for trigram matching. The one that we opted for was pretty slow to generate, like 15 to 20 minutes for 54 million records on a pretty substantial machine. And it's nice because you can use order by on it and just take the top results. You don't have to calculate the similarity of everything in the table. But the problem is you can do that only on one column. So in our case, we really wanted to do that on both the first name and last name. So that did not work very well for us on this project.

[25:38] So what was the solution that we ended up on, and how did that work for us? This is a lot of code on the screen. We'll blow it up a little bit more. Just to reiterate what we saw earlier, first we need to annotate the first name and the last name—or excuse me, annotate the queryset with the Daitch-Mokotoff codes for the first name and the last name. And then do the same thing for the edit distance. Just anecdotally, we found that an edit distance of two seemed, for the most part, to include the true typos and eliminate the majority of the false positives. So that's what we settled on.

[26:24] And then you can filter on that here. This is a live cell, so I can run it, and we get back the 18 rows for Sonia Rosia. So that's what that would look like in Django. Of course, none of this would be possible or fast without good indexes. We settled on a case-insensitive starts-with matching, again just enabling the operators to find records as quickly as possible.

[27:10] And there's a special ops class for the index in Postgres, this text_pattern_ops, that gives you really fast index matching on two columns at once. In our case, we may even add a third in here, but for the demo we just have first name and last name. And then, second, we can create separate indexes for the Soundex code and the Daitch-Mokotoff code on the first name and last name as well, for the phonetic matching that we talked about. No talk would be anxiety-inducing enough without a live demo.

[27:58] So here we go. And since it's Gerald's first talk, I thought we should let him do the demo. Cool.

**Speaker 2:** [28:13] All right. Is that big enough for y'all, or could you make it bigger? Okay, cool. I just thought of this idea, but I thought it would be cool to—nope. Just show the John Doe example that we showed earlier. So that was John Doe. In the video we saw earlier, you saw how long it took. But with this new prefix, you can see it took 60 milliseconds. So, big improvement.

[28:58] But to showcase some of the other search algorithms that we also work with, I'm gonna use one of the cases that Tobias was showcasing. Yes. So that's just the last name. And as you notice, as you type, it's searching. As soon as characters are typed, it's pulling in results. So if we just go for SON, you can see it pulled in 17 results in 16, in 15 milliseconds. Let's go Son; you get Sonya, Sonny, and this Ed Sonya. So this is the full name

[29:45] on exact prefix. So it pulls in the names exactly as they're typed into the name fields. You can see we got 14 names returned, which matches what Tobias was showcasing in his Jupyter notebook. But we might be missing some. So if we apply the Daitch-Mokotoff, which is again the sounds-like search algorithm, now we're up to 200. A lot of these are false positives, but a lot of these names will sound like the name that we've entered. So let's see if I can find—well, I'll use the other example for that.

[30:30] So these are all things that might sound like Sonya Reas, but the Levenshtein will reduce all that noise. It'll apply a scoring to all the names that were returned to more closely match what was entered in the first and last name field. So if we apply that search, we narrow down to 18. So you can see we gained some by applying this search algorithm as well. So now we're up to a total of 18 records returned. As you can see here, it kind of shows you what was added. With the Daitch-Mokotoff added, you can see that it picked up this name here: Soney Ries, Sona Ries.

[31:17] Sonia Ries, which was spelled slightly differently. So you can see how it will open the net to make sure that you can include names that you might be looking for, and then also close in on more specific names that you might be searching for. I can also demo another example. Let's clear this. We'll go with Celine, I think. I'm already down with the E-S-E.

[32:05] There you go. So for this person here, again, exact prefix. We got 15 results returned, matching exactly what I entered. Also, if you want to cast a wider net to get more sounds-like results, I apply Daitch-Mokotoff. And wait. Of course. I don't know what happened here. What happened?

[33:00] Oh you restarting it. Yeah.

**Tobias McNulty:** [33:10] Yeah, and this is just running on my laptop; it's not on a big fancy server somewhere. Now we do it live. Yeah.

**Speaker 2:** [33:33] Is he hosting?

**Tobias McNulty:** [33:34] We found a bug. I don't know. Too many names or something.

**Speaker 2:** [33:39] Yeah.

**Tobias McNulty:** [33:40] Try it in that.

**Speaker 2:** [33:43] Yeah. Okay. All right, so ignore that. But as you can see, if we apply it to Levenshtein, initially the Daitch-Mokotoff would—before I got in front of people, it showed 200 results. A lot of those results were false positives. So, for example, you might have seen the first name spelled C-O-L-I-N, which could be Celine or it could be Colin, however you want to say it. You have examples like that; those are false positives in the set that the attorneys might not want. So applying this Levenshtein sort of weeds those out. So we would go from 200 to 34 results.

[34:28] And in a case like this, let's say, for instance, you do know the birth date of the person who's looking for expungements. You will select their birth date. That will whittle it down to a set of results that you are looking for. So, best to have a summary.

**Tobias McNulty:** [34:50] Yeah. And just to emphasize, this isn't a user interface that we would recommend giving to a customer. This is more for folks who are curious about these things to play around with them and understand how they work and how you can layer them to achieve a result that makes sense for the data sets that you have. In our case, we always started with exact prefix matching, so there wasn't any other option to start with. And then we added a button that would limit it to similar names based on the combination of Daitch-Mokotoff and Levenshtein.

[35:36] So we don't require the user to understand everything about these algorithms in order to use the application. We have maybe five minutes for questions. Yeah, really briefly, just to summarize: we're not experts in these algorithms. We're just sharing a story of what worked for us. I had to look up how to pronounce one of them today before the talk. But the few wins that we found in working on this project were the text_pattern_ops indexes in Postgres for really fast starts-with matching in Django.

[36:21] One thing we didn't mention earlier is that if you can avoid sorting, or delay sorting until as late as possible. Postgres, of course, will return the top N records much faster if it doesn't have to sort everything below that. So that's another strategy that we used. And, like Gerald demoed, you can use Soundex or Daitch-Mokotoff to broaden the search, and then Levenshtein less than or equal to to remove some of those false positives. One hiccup that we hit is just making sure that the indexes that you create match exactly the function calls that you're using. And again, Postgres is really amazing that we could take this 20-second query that the user was experiencing and turn that into a

[37:10] live search that comes back in under 10 milliseconds. It felt like a pretty big win, so we were very proud of that. And yeah, that's all we had. There's a few links here to the demo site. This is deployed to a little 16 GB DigitalOcean instance if you want to try to break it, or figure out why it broke just now. And the Git repo, and there's a companion blog post up in the Cactus blog as well. So thanks very much. I'm not sure—we have a few minutes for questions if folks have any. Seven minutes, okay.

[37:56] Do you want to? Okay, cool.

**Speaker 3:** [38:11] One of my problems when I've done ETL on names is they get entered in the wrong field, like John Roberts comes in as Robert Johns, and J. Edgar Hoover comes in as just Edgar Hoover. For your client, did you take these into account?

**Tobias McNulty:** [38:35] Yeah, I don't know if we observed that particular issue or not. We definitely did observe some similar inconsistencies. Nicknames were one thing that we didn't touch on specifically in this talk that we did have to deal with in the project. We decided in the end not to try to solve that problem in a magical way, but instead to enable the user with a tool that was so fast—and they're fairly expert users, they're attorneys—that they could find records like that.

[39:21] So if a full name was entered in one field, or they were swapped like you're talking about, they could test that out or quickly find it.

**Speaker 4:** [39:36] Thank you for sharing your findings. I'm next. I was curious; maybe I didn't follow. I tried to pay close attention, but maybe I didn't follow. What was the correlation between having that index and having to use this method that just matches the first few letters at the beginning of the word, right? Yeah, there seems to be this correlation between applying the index and then you have to use this starts-with.

**Tobias McNulty:** [40:04] Yeah, and starts-with matching works pretty well for names, so we didn't explore too much, like contains matching. But I'm not aware of the underlying details, but we definitely confirmed that it is slow or does not use the correct index if you don't use this opclass, which, to my knowledge, I had not come across before in a Postgres or Django project. So yeah, that was one win for us. Thanks.

**Speaker 5:** [40:42] Just curious, I know with names, not every edit distance is the same. So let's say John D. Smith versus John Smith is edit distance one, and that probably isn't the same person because it's a very common name. But then, like, I'm Paul Zrotsky, Z-K-I. If you see us misspelling S-K-I, it's probably the same person, kind of thing. So I know there's other—I think Splink is one for probabilistic matching for that kind of category of problems. Did you encounter that, or would that be an extension to this? I'm selfishly curious too, because it'd be cool to see it in Django.

**Tobias McNulty:** [41:16] What was the—

**Speaker 5:** [41:18] Probabilistic matching, and Splink, I believe, is one library. I don't know if it has an integration with Postgres; I forget.

**Tobias McNulty:** [41:26] Okay.

**Speaker 5:** [41:26] But yeah.

**Tobias McNulty:** [41:27] Yeah, I don't think we came across that on this project. So, good to know.

**Speaker 6:** [41:38] I'm hoping I'm getting this right. For the Levenshtein less-than-or-equal-to, you mentioned that you had some limitations with that approach only because there was one field that you could use ORDER BY on. Is that right?

**Tobias McNulty:** [41:57] Sorry, that was with the trigram matching.

**Speaker 6:** [42:05] Wondering if you considered maintaining a shadow full-name field, with an overridden save method or something of that nature, in a normal form, maybe separated by a hyphen or some other character that's not normally in a name, and then using that one field as a way to sort of speed up the index creation. I mean, did you play around with—

**Tobias McNulty:** [42:36] With trigrams, you're saying to combine the name together and then use that? Yeah, that's a good idea. We did not explore that in the project. So thanks.

**Speaker 7:** [42:57] Thank you very much. Mazing Gaffel from New Zealand. I have nothing to do with this conference. I'm a public health physician. But usually I attend this conference to find ideas and to hunt for ideas. And today I got a lot of ideas from this conference. When you do the fuzzy match, you will get a score with it. And the score will decide: is it 50% matched, is it 90% matched, is it 80% matched? What's the cutoff point which you used in your software?

**Tobias McNulty:** [43:40] Yeah, we did think about trying to score the results in some way and be, I don't know, more complete or—I'm not sure what the right word is—but really try to come up with the exact right result that the user is looking for. Take some guesses and come up with the right result that we think the user is looking for. In the end, just because of the expert nature of the users on this particular project, we wanted to keep it as simple as possible and enable them to try these different combinations on their own. But that's another good idea, a great idea, to try to create a score out of these and, depending on the confidence, build a result

[44:26] set based on that. Thanks.

**Speaker 8:** [44:31] Okay, that's all the time we have for questions. Please give a hand to Tobias and Gerald.
