2

I want create index with dynamic template and turn analyzing off for string fields. I have created query for elastic search, but how to translate it into elastic4s statments? (version elastic4s 1.3.x is preffered)

The statement is:

PUT /myIndex
{
    "mappings": {
        "myType": {
            "dynamic_templates": [
                {
                    "templateName": {
                        "match":              "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type":           "string",
                            "index" : "not_analyzed",
                            "omit_norms" : true
                    }
                  }
                }
            ]
}}}

P.S.

May be it is possible to create this index by executing this "raw" request, but I did not find how to do that with elastic4s 1.3.4 :(

Cherry
  • 31,309
  • 66
  • 224
  • 364

2 Answers2

2

Elastic4s (as of 1.5.4) supports dynamic templates when creating indexes. So you can do something like:

 val req = create.index("my_index").mappings(
    "my_type" templates (
      template name "es" matching "*_es" matchMappingType "string" mapping {
        field withType StringType analyzer SpanishLanguageAnalyzer
      },
      template name "en" matching "*" matchMappingType "string" mapping {
        field withType StringType analyzer EnglishLanguageAnalyzer
      }
    )
  )

So the equivalent of the example you posted would be:

  create.index("my_index").mappings(
    "my_type" templates (
      template name "templateName" matching "*" matchMappingType "string" mapping {
        field typed StringType index NotAnalyzed omitNorms true
      }
  )
sksamuel
  • 16,154
  • 8
  • 60
  • 108
2

Sometimes it's easier to manage your mapping in raw JSON. You can put the raw JSON on a file to make it possible to be updated without need to rebuild your application. If you want to use this raw JSON to create the index you can do something like this:

client.execute {
    create index "myIndex" source rawMapping
}

where rawMapping is the string with your raw JSON content.

Bruno dos Santos
  • 1,361
  • 1
  • 12
  • 21