Vynder
Vynder

Reputation: 655

Language Analyzers and Synonyms on same field in elasticsearch

I have a German analyzer on fields title and description (and it work fine for me)

"mappings": {
    "item" : {
      "properties" : {
        "title" : {
          "type" :    "string",
          "analyzer": "german"
        },
        "description" : {
          "type" :   "string",
          "analyzer": "german"
        }
      }
    }
  } 

But now I tried to add synonyms. How I can add two analyzers on same field?

Upvotes: 0

Views: 525

Answers (1)

Mysterion
Mysterion

Reputation: 9320

You couldn't add two analyzers for 1 field. What you could do is to describe custom analyzer, which use synonyms filter inside and german specifics filter combined with needed tokenizer, so basically you need to mix everything you need in a custom way.

One could imagine something like this (a very rough example):

PUT /my_index
    {
      "settings": {
        "analysis": {
          "filter": {
            "german_stop": {
              "type":       "stop",
              "stopwords":  "_german_" 
            },
            "german_stemmer": {
              "type":       "stemmer",
              "language":   "light_german"
            },
            "my_synonyms": {
              "type": "synonym", 
              "synonyms": [ 
                "british,english",
                "queen,monarch"
              ]
            }
          },
          "analyzer": {
            "german": {
              "tokenizer":  "standard",
              "filter": [
                "lowercase",
                "german_stop",
                "my_synonyms",
                "german_normalization",
                "german_stemmer"
              ]
            }
          }
        }
      }
    }

In a filter chain, you need to specify all filters you want to include - stemmers, synonyms, stop words, lowercase, etc, etc. (also keep in mind, that order is matters), and the use it inside mappings, as you described in your question.

Later you could test your analyzer by running

GET /_analyze
{
  "analyzer": "german",
  "text": "Darf ich mit Bargeld bezahlen?"
}

Upvotes: 1

Related Questions