Phonetic Matchingedit

In a last, desperate, attempt to match something, anything, we could resort to searching for words that sound similar, even if their spelling differs.

Several algorithms exist for converting words into a phonetic representation. The Soundex algorithm is the granddaddy of them all, and most other phonetic algorithms are improvements or specializations of Soundex, such as Metaphone and Double Metaphone (which expands phonetic matching to languages other than English), Caverphone for matching names in New Zealand, the Beider-Morse algorithm, which adopts the Soundex algorithm for better matching of German and Yiddish names, and the Kölner Phonetik for better handling of German words.

The thing to take away from this list is that phonetic algorithms are fairly crude, and very specific to the languages they were designed for, usually either English or German. This limits their usefulness. Still, for certain purposes, and in combination with other techniques, phonetic matching can be a useful tool.

First, you will need to install the Phonetic Analysis plug-in from https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-phonetic.html on every node in the cluster, and restart each node.

Then, you can create a custom analyzer that uses one of the phonetic token filters and try it out:

PUT /my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "dbl_metaphone": { 
          "type":    "phonetic",
          "encoder": "double_metaphone"
        }
      },
      "analyzer": {
        "dbl_metaphone": {
          "tokenizer": "standard",
          "filter":    "dbl_metaphone" 
        }
      }
    }
  }
}

First, configure a custom phonetic token filter that uses the double_metaphone encoder.

Then use the custom token filter in a custom analyzer.

Now we can test it with the analyze API:

GET /my_index/_analyze?analyzer=dbl_metaphone
Smith Smythe

Each of Smith and Smythe produce two tokens in the same position: SM0 and XMT. Running John, Jon, and Johnnie through the analyzer will all produce the two tokens JN and AN, while Jonathon results in the tokens JN0N and ANTN.

The phonetic analyzer can be used just like any other analyzer. First map a field to use it, and then index some data:

PUT /my_index/_mapping/my_type
{
  "properties": {
    "name": {
      "type": "string",
      "fields": {
        "phonetic": { 
          "type":     "string",
          "analyzer": "dbl_metaphone"
        }
      }
    }
  }
}

PUT /my_index/my_type/1
{
  "name": "John Smith"
}

PUT /my_index/my_type/2
{
  "name": "Jonnie Smythe"
}

The name.phonetic field uses the custom dbl_metaphone analyzer.

The match query can be used for searching:

GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "name.phonetic": {
        "query": "Jahnnie Smeeth",
        "operator": "and"
      }
    }
  }
}

This query returns both documents, demonstrating just how coarse phonetic matching is. Scoring with a phonetic algorithm is pretty much worthless. The purpose of phonetic matching is not to increase precision, but to increase recall—to spread the net wide enough to catch any documents that might possibly match.

It usually makes more sense to use phonetic algorithms when retrieving results which will be consumed and post-processed by another computer, rather than by human users.