Fuzzy Search with ArangoSearch

Loosely match strings to find partial congruences and to compensate for typing errors

Fuzzy search is an umbrella term for various approximate matching algorithms. What they allow you to do is to find matches even if the search terms are not spelled exactly like the words in the stored text. This will include terms that are similar, alternatively spelled, or mistyped but potentially relevant for the search request as well.

If you want to try out fuzzy search with ArangoDB for yourself, then check out our interactive Fuzzy Search tutorial.

Similarity Measures

How the approximate string matching algorithms usually work is that they measure how similar (or different) two strings are. The following similarity measures are available in ArangoDB:

  • Levenshtein distance and a variant called Damerau-Levenshtein distance, which work best with short strings
  • n-gram similarity and a variant called positional n-gram similarity, that are well-suited to assess the similarity of longer strings that share subsequences

Levenshtein Distance

The Levenshtein distance is an edit distance string metric. It is informally defined as the minimum number of single-character edits to go from one string to the other. The allowed operations are:

  • insertions
  • deletions
  • substitutions

The lowest possible number of the required operations is the Levenshtein distance. Consider the following two strings:

  1. galaxy
  2. galxy

The latter word is misspelled (it is missing the second a). We can correct it by adding an a. One operation in total means that the Levenshtein distance is 1. It also is for the following pair of strings, but this time requiring the removal of a character (an extra l):

  1. galaxy
  2. gallaxy

In the following example, the e needs to be replaced with an a:

  1. galaxy
  2. gelaxy

This is again one operation, so the edit distance is 1. Here is an example that requires more than one operation:

  1. galaxy
  2. glaaxy

The first a and the l are in the wrong order, perhaps cause the latter string was entered hastily. We can remove the l from the second position and insert an l at the third position to correct the string. Therefore, the Levenshtein distance is 2. We could also substitute the second and third character instead, but the edit distance remains the same.

Damerau-Levenshtein Distance

The Damerau-Levenshtein distance is like the Levenshtein distance but with transpositions as additional operation. Consider the following example where the latter string has the a and l in the wrong order:

  1. galaxy
  2. glaaxy

This can be corrected by shifting the l one character to the right. Hence, the Damerau-Levenshtein distance is only 1. The pure Levenshtein distance is 2.

N-Gram Similarity

The n-grams of a string are all of its possible substrings of a given length. The n in n-gram stands for that substring size. N-grams of size 2 are called 2-grams or bigrams, and n-grams of size 3 are called 3-grams or trigrams.

Here is an example for the word avocado that has three trigrams:

  1. avocado
  2. avo
  3. voc
  4. oca
  5. cad
  6. ado

To compare the similarity of two sets of n-grams, one can simply count how many n-grams of the target string match the source string (the target string being the search term and the source string being the stored string), and divide the sum by the number of the target string’s n-grams. Only fully matching n-grams count.

Consider the following example with avocado as source string and vocals as target string:

  1. avocado vocals
  2. avo voc
  3. voc oca
  4. oca cal
  5. cad als
  6. ado

We find the trigrams voc and oca from the right side also on the left side. The other two do not have a corresponding trigram on the left side. That means, 2 / 4 trigrams match, resulting in a similarity of 0.5 using this similarity measure.

Positional N-Gram Similarity

Instead of considering only full n-gram matches, one can also consider partial matches where the characters are in the same positions and counting the longest common sequence.

  1. avocado vocals
  2. avo voc
  3. voc oca
  4. oca cal
  5. cad als
  6. ado

In above example, voc and oca on the right side have a fully matching trigram on the left side. cal and als do not, but there is cad which has a c in the first position and an a in the second position, and both avo and ado have a matching a in the first position.

  1. voc voc 3 / 3 = 1
  2. oca oca 3 / 3 = 1
  3. cad cal 2 / 3 = 0.666
  4. ado als 1 / 3 = 0.333

We sum up the highest similarity we found for each trigram and divide by the total n-gram count, which ever of the two is higher (here the left side with 5 trigrams). The result is 3 / 5 or 0.6 in positional n-gram similarity.

Fuzzy Search in ArangoDB

There are the following AQL string functions to calculate the similarity of two arbitrary strings:

  1. RETURN [
  2. LEVENSHTEIN_DISTANCE("galaxy", "glaaxy"), // 1 (with transpositions)
  3. NGRAM_SIMILARITY("avocado", "vocals", 3) // 0.5 (using trigrams)
  4. NGRAM_POSITIONAL_SIMILARITY("avocado", "vocals", 3) // 0.6 (using trigrams)
  5. ]

To perform fuzzy searches, there are two ArangoSearch functions:

They can be used in conjunction with a View for index-accelerated fuzzy search queries. The string similarity affects the overall BM25() / TFIDF() score when ranking results.

Searching with LEVENSHTEIN_MATCH()

Dataset: IMDB movie dataset

Custom Analyzer:

Create a text Analyzer in arangosh to tokenize text, normalize case to all lowercase and to remove diacritics, but with stemming disabled unlike the built-in text_en Analyzer. The search will be accent- and case-insensitive, and the Levenshtein distance between the stored and searched text will be taken into account accurately. With stemming enabled, it would be less accurate with respect to the original strings, but potentially find more matches that are also relevant.

  1. //db._useDatabase("your_database"); // Analyzer will be created in current database
  2. var analyzers = require("@arangodb/analyzers");
  3. analyzers.save("text_en_no_stem", "text", { locale: "en.utf-8", accent: false, case: "lower", stemming: false, stopwords: [] }, ["frequency", "norm"]);

View definition:

  1. {
  2. "links": {
  3. "imdb_vertices": {
  4. "fields": {
  5. "description": {
  6. "analyzers": [
  7. "text_en_no_stem"
  8. ]
  9. }
  10. }
  11. }
  12. }
  13. }

AQL queries:

Search for the token galxy in the movie descriptions with some fuzziness. The maximum allowed Levenshtein distance is set to 1. Everything with a Levenshtein distance equal to or lower than this value will be a match and the respective documents will be included in the search result. The query will find the token galaxy as the edit distance to galxy is 1:

  1. FOR doc IN imdb
  2. SEARCH ANALYZER(
  3. LEVENSHTEIN_MATCH(
  4. doc.description,
  5. TOKENS("galxy", "text_en_no_stem")[0],
  6. 1, // max distance
  7. false // without transpositions
  8. ),
  9. "text_en_no_stem"
  10. )
  11. SORT BM25(doc) DESC
  12. RETURN {
  13. title: doc.title,
  14. description: doc.description
  15. }
titledescription
Stargate: The Ark of Truth… in the Ori’s own home galaxy. … SG-1 travels to the Ori galaxy aboard the Odyssey. … finds themselves in a distant galaxy fighting two powerful enemies.
The Ice Pirates… the most precious commodity in the galaxy is water. … the unreachable centre of the galaxy at the end of the galactic trade wars. The galaxy is ruled by an evil emperor …
Star Wars: Episode III: Revenge of the Sith… leading a massive clone army into a galaxy-wide battle against the Separatists. When the sinister Sith unveil a thousand-year-old plot to rule the galaxy, …

Searching with NGRAM_MATCH()

Dataset: IMDB movie dataset

Custom Analyzer:

NGRAM_MATCH() requires an ngram Analyzer. For this example, create a trigram Analyzer in arangosh with a minimum and maximum n-gram size of 3, not including the original string:

  1. //db._useDatabase("your_database"); // Analyzer will be created in current database
  2. var analyzers = require("@arangodb/analyzers");
  3. analyzers.save("trigram", "ngram", { min: 3, max: 3, preserveOriginal: false, streamType: "utf8" }, ["frequency", "norm"]);

View definition:

  1. {
  2. "links": {
  3. "imdb_vertices": {
  4. "fields": {
  5. "name": {
  6. "analyzers": [
  7. "trigram"
  8. ]
  9. }
  10. }
  11. }
  12. }
  13. }

AQL queries:

Search for actor names with an n-gram similarity of at least 50%.

  1. FOR doc IN imdb
  2. SEARCH NGRAM_MATCH(
  3. doc.name,
  4. "avocado",
  5. 0.5, // similarity threshold
  6. "trigram" // custom n-gram Analyzer
  7. )
  8. RETURN {
  9. name: doc.name
  10. }
name
Nancy Savoca
John Savoca