Skip to content

Commit

Permalink
Add positiveScoreImpact parameter to RankFeaturesField (#3137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippus authored Aug 21, 2024
1 parent d786b3f commit 240575a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sksamuel.elastic4s.fields
object RankFeaturesField {
val `type`: String = "rank_features"
}
case class RankFeaturesField(name: String) extends ElasticField {
case class RankFeaturesField(name: String,
positiveScoreImpact: Option[Boolean] = None) extends ElasticField {
override def `type`: String = RankFeaturesField.`type`
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import com.sksamuel.elastic4s.fields.RankFeaturesField
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}

object RankFeaturesFieldBuilderFn {
def toField(name: String, values: Map[String, Any]): RankFeaturesField = RankFeaturesField(name)
def toField(name: String, values: Map[String, Any]): RankFeaturesField =
RankFeaturesField(
name,
values.get("positive_score_impact").map(_.asInstanceOf[Boolean])
)

def build(field: RankFeaturesField): XContentBuilder = {

val builder = XContentFactory.jsonBuilder()
builder.field("type", field.`type`)
field.positiveScoreImpact.foreach(builder.field("positive_score_impact", _))
builder.endObject()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,12 @@ class ElasticFieldBuilderFnTest extends AnyWordSpec with Matchers {
ElasticFieldBuilderFn(field).string shouldBe jsonString
ElasticFieldBuilderFn.construct(field.name, JacksonSupport.mapper.readValue[Map[String, Any]](jsonString)) shouldBe field
}

"support RankFeaturesField with positive_score_impact" in {
val field = RankFeaturesField("rank_features_field", positiveScoreImpact = Some(false))
val jsonString = """{"type":"rank_features","positive_score_impact":false}"""
ElasticFieldBuilderFn(field).string shouldBe jsonString
ElasticFieldBuilderFn.construct(field.name, JacksonSupport.mapper.readValue[Map[String, Any]](jsonString)) shouldBe field
}
}
}

0 comments on commit 240575a

Please sign in to comment.