-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Range aggregation. Stats aggregation. Global aggregation
- Loading branch information
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...n/kotlin/org/taymyr/lagom/elasticsearch/search/dsl/query/aggregation/GlobalAggregation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.taymyr.lagom.elasticsearch.search.dsl.query.aggregation | ||
|
||
data class GlobalAggregation( | ||
val aggs: Map<String, Aggregation> | ||
) : Aggregation { | ||
val global = mapOf<String, Any>() // Need to produce the global aggregation marker field "global": {} | ||
|
||
class Builder { | ||
private val aggs = mutableMapOf<String, Aggregation>() | ||
|
||
fun agg(name: String, aggregation: Aggregation): Builder = apply { aggs[name] = aggregation } | ||
fun build(): GlobalAggregation = GlobalAggregation(aggs) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun builder(): Builder = Builder() | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...in/kotlin/org/taymyr/lagom/elasticsearch/search/dsl/query/aggregation/RangeAggregation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.taymyr.lagom.elasticsearch.search.dsl.query.aggregation | ||
|
||
import java.math.BigDecimal | ||
|
||
data class RangeAggregation( | ||
val range: RangeAggregationSpec, | ||
val aggs: Map<String, Aggregation>? = null | ||
) : Aggregation { | ||
|
||
class Builder { | ||
private var field = "" | ||
private var keyed = false | ||
private val ranges = mutableListOf<RangeSpec>() | ||
private var stats = false | ||
private val aggs = mutableMapOf<String, Aggregation>() | ||
|
||
fun field(value: String): Builder = apply { field = value } | ||
fun keyed(value: Boolean): Builder = apply { keyed = value } | ||
fun range(from: BigDecimal? = null, to: BigDecimal? = null): Builder = apply { | ||
ranges += RangeSpec(from, to) | ||
} | ||
fun range(from: Int? = null, to: Int? = null): Builder = apply { | ||
ranges += RangeSpec(from?.toBigDecimal(), to?.toBigDecimal()) | ||
} | ||
fun stats(): Builder = apply { stats = true } | ||
fun agg(name: String, aggregation: Aggregation): Builder = apply { aggs[name] = aggregation } | ||
|
||
fun build(): RangeAggregation { | ||
if (field.isBlank()) error("'field' should not be blank") | ||
if (stats) { | ||
aggs[DEFAULT_STATS_AGG_NAME] = StatsAggregation(StatsAggregation.StatsFieldSpec(field)) | ||
} | ||
return RangeAggregation( | ||
RangeAggregationSpec( | ||
field, | ||
keyed, | ||
if (ranges.isEmpty()) error("'ranges' should not be empty") else ranges | ||
), | ||
if (aggs.isEmpty()) null else aggs.toMap() | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
const val DEFAULT_STATS_AGG_NAME = "agg_stats" | ||
|
||
@JvmStatic | ||
fun builder(): Builder = Builder() | ||
} | ||
} | ||
|
||
data class RangeAggregationSpec( | ||
val field: String, | ||
val keyed: Boolean = false, | ||
val ranges: List<RangeSpec> | ||
) | ||
|
||
data class RangeSpec( | ||
val from: BigDecimal?, | ||
val to: BigDecimal? | ||
) |
24 changes: 24 additions & 0 deletions
24
...in/kotlin/org/taymyr/lagom/elasticsearch/search/dsl/query/aggregation/StatsAggregation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.taymyr.lagom.elasticsearch.search.dsl.query.aggregation | ||
|
||
import java.math.BigDecimal | ||
|
||
data class StatsAggregation( | ||
val stats: StatsFieldSpec, | ||
val missing: BigDecimal? = null | ||
) : Aggregation { | ||
data class StatsFieldSpec(val field: String) | ||
|
||
companion object { | ||
@JvmStatic | ||
@JvmOverloads | ||
fun statsAggregation(field: String, missing: BigDecimal? = null) = StatsAggregation(StatsFieldSpec(field), missing) | ||
} | ||
} | ||
|
||
data class StatsAggregationResult( | ||
val count: Int, | ||
val min: BigDecimal?, | ||
val max: BigDecimal?, | ||
val avg: BigDecimal?, | ||
val sum: BigDecimal? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters