Skip to content

Commit

Permalink
Merge pull request #142 from mixify/sum-aggregation
Browse files Browse the repository at this point in the history
Adds Sum Aggregation support
  • Loading branch information
jillesvangurp committed May 21, 2024
2 parents 68e29a0 + f3d5e72 commit 6345bef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,14 @@ fun Aggregations?.topHitResult(name: String, json: Json = DEFAULT_JSON): TopHits
fun Aggregations?.topHitResult(name: Enum<*>, json: Json = DEFAULT_JSON): TopHitsAggregationResult =
getAggResult(name.name, json)

@Serializable
data class SumAggregationResult(
val value: Double,
)

fun Aggregations?.sumAggregationResult(name: String, json: Json = DEFAULT_JSON): SumAggregationResult =
getAggResult(name, json)

fun Aggregations?.sumAggregationResult(name: Enum<*>, json: Json = DEFAULT_JSON): TopHitsAggregationResult =
getAggResult(name.name, json)

Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,14 @@ class AggQueryTest : SearchTestBase() {
b.topHitResult("top").hits.hits.size shouldBeGreaterThan 0
}
}

@Test
fun sumAgg() = coRun {
before()
val response = repository.search {
resultSize = 0
agg("by_value", SumAgg(MockDoc::value)){}
}
response.aggregations.sumAggregationResult("by_value").value shouldBe 10.0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,19 @@ class FilterAgg(filter: ESQuery): AggQuery("filter") {
}
}

class SumAggConfig : JsonDsl() {
var field by property<String>()
}

class SumAgg(val field: String, block: (SumAggConfig.() -> Unit)? = null) : AggQuery("sum") {
constructor(field: KProperty<*>, block: (SumAggConfig.() -> Unit)? = null) : this(field.name, block)

init {
val config = SumAggConfig()
config.field = field
block?.invoke(config)
put(name, config)
}
}


0 comments on commit 6345bef

Please sign in to comment.