top | item 18582469

How Google Code Search Worked: Regex Matching with a Trigram Index (2012)

434 points| kbumsik | 7 years ago |swtch.com | reply

70 comments

order
[+] ravenstine|7 years ago|reply
This reminds me of a browser-side search I once wrote using a combination of regex, Soundex, and Damerau-Levenshtein. The reason I made this browser-side is because the app doesn't have that many records that it needed to be on the server; the records were in the hundreds to thousands, not millions.

https://github.com/SCPR/fire-tracker/blob/master/app/lib/sea...

Not the best code I've ever written by far, but it was a neat little experiment.

Basically, indexing each record with Soundex(phonetics) makes the search semi-tolerant to misspellings and also brings up similar-sounding results, while Damerau-Levenshtein is used to sort the results by closeness. I also had it index timestamps with

Here it is in action:

https://firetracker.scpr.org/archive

As you can see, you can misspell search terms and it will usually get it right. Granted, a lot of unrelated records usually come up in a search, but the important thing is the user should get what they're looking for in the first few results, usually the first.

Regex is a powerful thing.

[+] softwaredoug|7 years ago|reply
Very cool! - Most search problems are solved this way - interesting index data modeling to the needed use cases, not magic machine learning neural network unicorns. Many want to sell you the latter. The thing that gets the job done is usually the former.

One big reasons for this is the need for really good training data to do supervised learning on search problems. If you’re Google or Wikipedia, you can get this data. But many struggle to understand what a good result was for a query based on clicks and other user data due to low volumes of traffic.

[+] kqr|7 years ago|reply
Then you're using magic machine learning wrong. I'm part of a small team who uses it exactly for the purpose of giving intelligent search to non-Googles and non-Wikipedias, and we have a very good track record.

The key is to use machine learning to generalize user behavior across multiple items in roughly the same category, rather than individual items. In fact, with tiny amounts of data you should pretty much never deal in individual items -- always in groups of related items.

[+] kierenj|7 years ago|reply
I'm gonna be that guy and ask you not to refer to OCD in a flippant/casual sense please
[+] gauravphoenix|7 years ago|reply
>Most search problems are solved this way - interesting index data modeling to the needed use cases, not magic machin learning neural network unicorns. Many want to sell you the latter. The thing that gets the job done is usually the former.

occam's razor

[+] btown|7 years ago|reply
Are there any good writeups on how to implement the trigram index side of this? For instance, the code alludes to the idea that you could store each trigram as a bitmap over documents, then the Boolean combinations become bitwise operations. I suppose you could keep the bitmaps in compressed (or even probabilistic sketch) form, then decompress only those required for a query, but is this the right intuition? Are there lower-level libraries or databases that do this kind of thing well, rather than rolling your own on a KV store or using something monstrous like ElasticSearch?
[+] hn_throwaway_99|7 years ago|reply
Note that Elasticsearch is built on top of Lucene (lucene.apache.org), which is a straight forward search indexer that creates a reverse index and document store, and gives pluggable low-level access to parts of the indexer path. Would be very easy to create a custom analyzer to create a trigram-based index (my guess is one already exists).
[+] karmakaze|7 years ago|reply
How fitting. I've been working on making Etsy houndd [0] available as SaaS. Houndd uses a trigram index with regexp and can search all the repos I care about in ms.

The demo page works. The signup just send me a confirmation email and the setup is still manual, so expect hours/day delay. Aiming to have this all automated by tomorrow. Appreciate any/all feedback.

[0] https://github.com/etsy/hound [1] https://gitgrep.com

[+] petters|7 years ago|reply
I wish GitHub had decent search functionality.
[+] hoorayimhelping|7 years ago|reply
I can't believe they don't adopt something like https://github.com/etsy/hound. It's all right there, it's super easy to setup, and it's a game changer. Lightning fast search that you can link to. Check it out for your org, I was at Etsy when Kelly wrote it, and I've taken it with me to other jobs.
[+] espeed|7 years ago|reply
Now that Microsoft has acquired GitHub, no doubt they will be upgrading GitHub search with SOTA algos.
[+] enriquto|7 years ago|reply
you mean, searching the past history of all codes?
[+] joatmon-snoo|7 years ago|reply
FWIW kythe.io is the modern successor that does this internally at Google. Haven't worked on it, but have written some code that's a client. Unfortunately I think the indexing pipelines aren't publically available.
[+] techbio|7 years ago|reply
Trigrams are of three characters, not tokens.

This is not the obvious search strategy for code, lacking semantic structure, though apparently it suits regex searchable indexes.

[+] crawshaw|7 years ago|reply
Trigram is defined in the dictionary as "a group of three consecutive written units such as letters, syllables, or words." Thus using the term for a triplet of tokens seems appropriate.
[+] dmoy|7 years ago|reply
See kythe.io for the semantic bits.
[+] sn41|7 years ago|reply
For a reasonable code base, say the linux kernel source code, how large is the trigram index? Is it necessary that the index be kept in memory?
[+] nestorD|7 years ago|reply
77MB for the Linux 3.1.3 kernel sources according to the "Implementation" section of the article.
[+] was_boring|7 years ago|reply

[deleted]

[+] softwaredoug|7 years ago|reply
Deleted the link

I did write a book on the topic, developed the Elasticsearch learning to rank plugin, and have helped several Alexa top 50 sites deliver more accurate search

I’m only pointing out that search is oversold industry, but when you read about what Google actually does it’s more normal stuff that we would all probably do to solve a similar problem.

(for those curious it was a link to content at a community conference i help run - pointing out how hard ML can be for search. I admit a bit tangential perhaps)

[+] z3t4|7 years ago|reply
Modern servers have a lot of compute resources and memory compared to 15 years back, before doing any optimizations I would start out with a naive full text search, which will most likely be fast enough. A full code search using regex is also more powerful for users. Imagine if you could use Regex when searching on Github or Google!? Source code use relative little space, If you strip out the markup/JS from web sites, they also take up relative little space. The only problem however is to educate users on how to do effective searches.
[+] humbledrone|7 years ago|reply
I think you might be underestimating the size of the document corpus that you'd be running over.
[+] cobookman|7 years ago|reply
Even with modern computers doing a naive regex search on 1GiB+ of full text would take a while. Memory isn't that fast. The trigrams helps you avoid needing to read every document on every search.

Even a traditional rdbms will start become slow with datasets in the 10-100GiB range.

Note that products like bigquery, snowflake, redshift...etc might be able to support but that is not Cheap or relatively fast