5

I have a document that I want to index on elasticSearch, this document contains some dynamic keys that I can not know in advance, like "spanish" or "french" in the following example

"contents": {
    "title": {
        "spanish": "Hola amigos",
        "french" : "Bonjour les amis"
    }
}

I'm using the elastic4s DSL in order to make my mapping (Via the createIndex DSL), but I can not find how to create, with this library, a dynamic mapping based on the "match" option ( like suggested here), in order to specify a different analyzer for each language stored under the path contents.title.

Any track please ? :-)

ylos
  • 522
  • 1
  • 6
  • 16

2 Answers2

2

You can't do what you want in the elastic4s Scala API at the moment. But you can create a dynamic template in the Java API and use the Scala API for the rest of your calls.

The dynamic template can be set to use a regex and apply a mapping. But since you will really know your countries in advance (there's only ~200) why not just spend the time and make a mapping?

sksamuel
  • 16,154
  • 8
  • 60
  • 108
  • `dynamicTemplates(template name "NotAnalyzed" 'match' "*" matchMappingType "string" mapping("type" typed StringType index NotAnalyzed))` leads to StackOveflow at `MappingDefinition.scala:141` any idea what is wrong? – lisak Jun 15 '15 at 23:25
  • this is correct `templates(Seq(template name "NotAnalyzed" 'match' "*" matchMappingType "string" mapping("type" typed StringType index NotAnalyzed)):_*)` anything else you pass in there : Iterable or single instance fails on StackOverFlow – lisak Jun 15 '15 at 23:34
-1

This is how the elastic4s documentation says to do it : (https://github.com/sksamuel/elastic4s/blob/master/guide/index.md)

val settings = ImmutableSettings.settingsBuilder().put("cluster.name","blah")
val client = ElasticClient.remote("ip.address",portNum)

client.execute {
 index into "indexname/indexType" doc StringSource(jsonDoc.toString)
}

where StringSource extends DocumentSource If you able to marshall your input into JSON, this is the best way to do it.

Ramdev
  • 375
  • 1
  • 3
  • 12
  • The OP is asking about mappings not indexing. – Cole Stanfield Oct 13 '16 at 05:29
  • As identified correctly, this response is for indexing document not so much addressing the issue of using templates for mapping to resolve the problem. The intent was however that when new fields are they would get created. However I had missed the custom analyzer depending on field name bit. Thanks @ColeStanfield for pointing that out. – Ramdev Oct 14 '16 at 12:04
  • No problem but if you want to edit your answer to better align with the question, I'll be happy to put the point back up. – Cole Stanfield Oct 14 '16 at 21:23