Giter VIP home page Giter VIP logo

elasticsearch-phonetic-fuzzy-example's Introduction

Phonetic + Fuzzy Search in Elasticsearch for retrieving relevant last names

This example illustrates the combination of a phonetic search with a fuzzy search to find last names similar to those typed in a search query.

As test data a list of most common german last names from wikipedia (https://de.wiktionary.org/wiki/Verzeichnis:Deutsch/Liste_der_häufigsten_Nachnamen_Deutschlands) is used. A snapshot of this list taken at 2016-06-10 is included in this repository as sample_data.txt

The python script run.py creates an elasticsearch index, fills it with the sample data and runs sample queries illustrating various options of searching last names phonetically similar to the search term.

Prerequisites

This script requires the elasticsearch python client installed on your machine. You can install this via pip by calling:

pip install elasticsearch

The script uses the localhost and port 9200 for elasticsearch and the index name 'phonetic_index' as default. You can change these values by editing the constants at the top of the script file.

The script also requires elasticsearch to run.

Details

The script creates the following index inside elasticsearch. The important point here is that we use the analyzer "haasephonetik" which is optimized for the german language.

PUT /phonetic_index
{
  "settings": {
    "analysis": {
      "filter": {
        "haasephonetik": {
          "type": "phonetic",
          "encoder": "haasephonetik"
        }
      },
      "analyzer": {
        "haasephonetik": {
          "tokenizer": "standard",
          "filter": "haasephonetik"
        }
      }
    }
  },
  "mappings": {
    "type": {
      "properties": {
        "text": {
          "type": "string",
          "index": "not_analyzed",
          "fields": {
            "phonetic": {
              "type": "string",
              "analyzer": "haasephonetik"
            }
          }
        }
      }
    }
  }
}

The script then evaluations the following queries:

Phonetic search only

GET /phonetic_index/type/_search
{
  "query" : {
    "match" : {
      "text.phonetic" : {
        "query" : "Meier"
      }
    }
  },
  "size" : 15
}

Phonetic search combined with fuzzy query with fuzziness 2

GET /phonetic_index/type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query": {
            "match": {
              "text.phonetic": {
                "query": "Meier"
              }
            }
          }
        }
      ],
      "should": [
        {
          "fuzzy": {
            "text": {
              "value": "Meier",
              "fuzziness": 2
            }
          }
        }
      ]
    }
  }
}

As you can see from the scripts' results, this query yields better results than the pure phonetic query.

elasticsearch-phonetic-fuzzy-example's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.