Search code examples
scalaelasticsearchmappingelastic4s

Elastic4s ngram mapping


I have to create an ElasticSearch mapping like this using elastic4s:

  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
       "type": "text",
       "analyzer": "ngram_analyzer",
       "fielddata": true
      },
      "lang": {
        "type": "keyword"
      },
      "order": {
        "type": "long"
      },
      "active": {
        "type": "boolean"
      }
      "description": {
        "type": "text"
      }
    }
  }

I can do

def mapping: Option[MappingDefinition] =
    Some(
      properties(
        KeywordField("id"),
        KeywordField("lang"),
        BasicField("order", "long"),
        BasicField("active", "boolean"),
        TextField("description")
      )
    )

for id, lang, order, active and description.
But, how can I do such mapping for name. the problem is analyzer and fielddata inside it.


Solution

  • You should use this:

    TextField("name").fielddata(true).analyzer("ngram_analyzer")
    

    You also need to make sure to properly create the ngram_analyzer in your index settings.