Skip to content

Commit

Permalink
[#2931]: add runtime to update mapping (#2972)
Browse files Browse the repository at this point in the history
Co-authored-by: Haikel magrahi <[email protected]>
Co-authored-by: Sam <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 9bf2d1f commit aaf3962
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sksamuel.elastic4s.requests.mappings
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.requests.analyzers.Analyzer
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.{DynamicMapping, DynamicTemplateRequest}
import com.sksamuel.elastic4s.requests.searches.RuntimeMapping

case class MappingDefinition(properties: Seq[ElasticField] = Nil,
all: Option[Boolean] = None,
Expand All @@ -20,7 +21,8 @@ case class MappingDefinition(properties: Seq[ElasticField] = Nil,
meta: Map[String, Any] = Map.empty,
routing: Option[Routing] = None,
templates: Seq[DynamicTemplateRequest] = Nil,
rawSource: Option[String] = None)
rawSource: Option[String] = None,
runtimes: Seq[RuntimeMapping] = Nil)
extends MappingDefinitionLike {

import com.sksamuel.elastic4s.ext.OptionImplicits._
Expand Down Expand Up @@ -68,4 +70,6 @@ case class MappingDefinition(properties: Seq[ElasticField] = Nil,
def dynamicTemplates(temps: DynamicTemplateRequest*): MappingDefinition = templates(temps)
def templates(temps: Iterable[DynamicTemplateRequest]): MappingDefinition = copy(templates = temps.toSeq)
def templates(temps: DynamicTemplateRequest*): MappingDefinition = copy(templates = temps.toSeq)
def runtimes(runtimes: Iterable[RuntimeMapping]): MappingDefinition = copy(runtimes = runtimes.toSeq)
def runtimes(runtimes: RuntimeMapping*): MappingDefinition = copy(runtimes = runtimes)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sksamuel.elastic4s.requests.mappings

import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.{DynamicMapping, DynamicTemplateRequest}
import com.sksamuel.elastic4s.requests.searches.RuntimeMapping

trait MappingDefinitionLike {
def all: Option[Boolean]
Expand All @@ -21,4 +22,5 @@ trait MappingDefinitionLike {
def routing: Option[Routing]
def templates: Seq[DynamicTemplateRequest]
def rawSource: Option[String]
def runtimes: Seq[RuntimeMapping]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.sksamuel.elastic4s.ext.OptionImplicits._
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.requests.analyzers.Analyzer
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.{DynamicMapping, DynamicTemplateRequest}
import com.sksamuel.elastic4s.requests.searches.RuntimeMapping

case class PutMappingRequest(indexes: Indexes,
properties: Seq[ElasticField] = Nil,
Expand All @@ -28,13 +29,14 @@ case class PutMappingRequest(indexes: Indexes,
routing: Option[Routing] = None,
templates: Seq[DynamicTemplateRequest] = Nil,
rawSource: Option[String] = None,
includeTypeName: Option[Boolean] = None)
includeTypeName: Option[Boolean] = None,
runtimes: Seq[RuntimeMapping] = Nil)
extends MappingDefinitionLike {

def all(all: Boolean): PutMappingRequest = copy(all = all.some)
def source(source: Boolean): PutMappingRequest = copy(source = source.some)

// the raw source should include proeprties but not the type
// the raw source should include properties but not the type
def rawSource(rawSource: String): PutMappingRequest = copy(rawSource = rawSource.some)

def sourceExcludes(sourceExcludes: String*): PutMappingRequest = copy(sourceExcludes = sourceExcludes)
Expand Down Expand Up @@ -85,4 +87,7 @@ case class PutMappingRequest(indexes: Indexes,

def includeTypeName(includeTypeName: Boolean): PutMappingRequest = copy(includeTypeName = includeTypeName.some)
def includeTypeName(includeTypeName: Option[Boolean]): PutMappingRequest = copy(includeTypeName = includeTypeName)

def runtimes(runtimes: Iterable[RuntimeMapping]): PutMappingRequest = copy(runtimes = runtimes.toSeq)
def runtimes(runtimes: RuntimeMapping*): PutMappingRequest = copy(runtimes = runtimes)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.elastic4s.handlers.index.mapping

import com.sksamuel.elastic4s.handlers.fields.ElasticFieldBuilderFn
import com.sksamuel.elastic4s.handlers.script.ScriptBuilderFn
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}
import com.sksamuel.elastic4s.requests.mappings.MappingDefinitionLike
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.DynamicMapping
Expand Down Expand Up @@ -65,6 +66,32 @@ object MappingBuilderFn {
d.parent.foreach(x => builder.startObject("_parent").field("type", x).endObject())
d.size.foreach(x => builder.startObject("_size").field("enabled", x).endObject())

if (d.runtimes.nonEmpty) {
builder.startObject("runtime")
d.runtimes.foreach { runtime =>
builder.startObject(runtime.field)
builder.field("type", runtime.`type`)

// format is only allowed with a type of date
runtime.format.foreach(builder.field("format", _))
runtime.script.foreach {
script => builder.rawField("script", ScriptBuilderFn(script))
}
if (runtime.fields.nonEmpty) {
builder.startObject("fields")
runtime.fields.foreach {
field =>
builder.startObject(field.name)
builder.field("type", field.`type`)
builder.endObject()
}
builder.endObject()
}
builder.endObject()
}
builder.endObject()
}

if (d.properties.map(_.name).distinct.size != d.properties.size)
throw new RuntimeException("Mapping contained properties with the same name")

Expand Down

0 comments on commit aaf3962

Please sign in to comment.