-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for percentiles aggregation #306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
id: elastic_aggregation_percentiles | ||
title: "Percentiles Aggregation" | ||
--- | ||
|
||
The `Percentiles` aggregation is a multi-value metrics aggregation that calculates one or more percentiles over numeric values extracted from the aggregated documents. | ||
|
||
In order to use the `Percentiles` aggregation import the following: | ||
```scala | ||
import zio.elasticsearch.aggregation.PercentilesAggregation | ||
import zio.elasticsearch.ElasticAggregation.percentilesAggregation | ||
``` | ||
|
||
You can create a `Percentiles` aggregation using the `percentilesAggregation` method this way: | ||
```scala | ||
val aggregation: PercentilesAggregation = percentilesAggregation(name = "percentilesAggregation", field = "intField") | ||
``` | ||
|
||
You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `Percentiles` aggregation using the `percentilesAggregation` method this way: | ||
```scala | ||
// Document.intField must be number value | ||
val aggregation: PercentilesAggregation = percentilesAggregation(name = "percentilesAggregation", field = Document.intField) | ||
``` | ||
|
||
If you want to specify the percentiles you want to calculate, you can use `percents` method: | ||
```scala | ||
val aggregationWithPercents: PercentilesAggregation = percentilesAggregation(name = "percentilesAggregation", field = Document.intField).percents(15, 50, 70) | ||
``` | ||
|
||
If you want to change the `missing`, you can use `missing` method: | ||
```scala | ||
val aggregationWithMissing: PercentilesAggregation = percentilesAggregation(name = "percentilesAggregation", field = Document.intField).missing(10.0) | ||
``` | ||
|
||
If you want to add aggregation (on the same level), you can use `withAgg` method: | ||
```scala | ||
val multipleAggregations: MultipleAggregations = percentilesAggregation(name = "percentilesAggregation1", field = Document.intField).withAgg(percentilesAggregation(name = "percentilesAggregation2", field = Document.doubleField)) | ||
``` | ||
|
||
You can find more information about `Percentiles` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-percentile-aggregation.html). | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,6 +210,48 @@ private[elasticsearch] final case class Multiple(aggregations: Chunk[SingleElast | |
aggregations.map(_.toJson).reduce(_ merge _) | ||
} | ||
|
||
sealed trait PercentilesAggregation | ||
extends SingleElasticAggregation | ||
with HasMissing[PercentilesAggregation] | ||
with WithAgg { | ||
|
||
/** | ||
* Sets the `percents` parameter for the [[zio.elasticsearch.aggregation.PercentilesAggregation]]. | ||
* | ||
* @param percents | ||
* a array of percentiles to be calculated for [[zio.elasticsearch.aggregation.PercentilesAggregation]] | ||
* @return | ||
* an instance of the [[zio.elasticsearch.aggregation.PercentilesAggregation]] enriched with the `percents` | ||
* parameter. | ||
*/ | ||
def percents(percent: Double, percents: Double*): PercentilesAggregation | ||
} | ||
|
||
private[elasticsearch] final case class Percentiles( | ||
name: String, | ||
field: String, | ||
missing: Option[Double], | ||
percents: Chunk[Double] | ||
) extends PercentilesAggregation { self => | ||
|
||
def missing(value: Double): PercentilesAggregation = | ||
self.copy(missing = Some(value)) | ||
|
||
def percents(percent: Double, percents: Double*): PercentilesAggregation = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any specific reason why we named this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In elastic api the parameter is named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed to leave this named |
||
self.copy(percents = Chunk.fromIterable(percent +: percents)) | ||
|
||
def withAgg(agg: SingleElasticAggregation): MultipleAggregations = | ||
multipleAggregations.aggregations(self, agg) | ||
|
||
private[elasticsearch] def toJson: Json = { | ||
val percentsField = | ||
(if (percents.nonEmpty) Some("percents" -> Arr(percents.map(_.toJson))) else None) ++ missing.map( | ||
"missing" -> _.toJson | ||
) | ||
Obj(name -> Obj("percentiles" -> (Obj("field" -> field.toJson) merge Obj(Chunk.fromIterable(percentsField))))) | ||
} | ||
} | ||
|
||
sealed trait SumAggregation extends SingleElasticAggregation with HasMissing[SumAggregation] with WithAgg | ||
|
||
private[elasticsearch] final case class Sum(name: String, field: String, missing: Option[Double]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ object AggregationResponse { | |
MinAggregationResult(value) | ||
case MissingAggregationResponse(value) => | ||
MissingAggregationResult(value) | ||
case PercentilesAggregationResponse(values) => | ||
PercentilesAggregationResult(values) | ||
case SumAggregationResponse(value) => | ||
SumAggregationResult(value) | ||
case TermsAggregationResponse(docErrorCount, sumOtherDocCount, buckets) => | ||
|
@@ -88,6 +90,14 @@ private[elasticsearch] object MissingAggregationResponse { | |
implicit val decoder: JsonDecoder[MissingAggregationResponse] = DeriveJsonDecoder.gen[MissingAggregationResponse] | ||
} | ||
|
||
private[elasticsearch] final case class PercentilesAggregationResponse(values: Map[String, Double]) | ||
extends AggregationResponse | ||
|
||
private[elasticsearch] object PercentilesAggregationResponse { | ||
implicit val decoder: JsonDecoder[PercentilesAggregationResponse] = | ||
DeriveJsonDecoder.gen[PercentilesAggregationResponse] | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at lines below (140+). You must adjust that too. Please, test this aggregation as sub aggregation of some other aggregation (you can add one it-test also). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And below at 171+. |
||
private[elasticsearch] final case class SumAggregationResponse(value: Double) extends AggregationResponse | ||
|
||
private[elasticsearch] object SumAggregationResponse { | ||
|
@@ -138,6 +148,8 @@ private[elasticsearch] object TermsAggregationBucket { | |
Some(field -> MinAggregationResponse(value = objFields("value").unsafeAs[Double])) | ||
case str if str.contains("missing#") => | ||
Some(field -> MissingAggregationResponse(docCount = objFields("doc_count").unsafeAs[Int])) | ||
case str if str.contains("percentiles#") => | ||
Some(field -> PercentilesAggregationResponse(values = objFields("values").unsafeAs[Map[String, Double]])) | ||
case str if str.contains("sum#") => | ||
Some(field -> SumAggregationResponse(value = objFields("value").unsafeAs[Double])) | ||
case str if str.contains("terms#") => | ||
|
@@ -169,6 +181,8 @@ private[elasticsearch] object TermsAggregationBucket { | |
(field.split("#")(1), data.asInstanceOf[MinAggregationResponse]) | ||
case str if str.contains("missing#") => | ||
(field.split("#")(1), data.asInstanceOf[MissingAggregationResponse]) | ||
case str if str.contains("percentiles#") => | ||
(field.split("#")(1), data.asInstanceOf[PercentilesAggregationResponse]) | ||
case str if str.contains("sum#") => | ||
(field.split("#")(1), data.asInstanceOf[SumAggregationResponse]) | ||
case str if str.contains("terms#") => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary new line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have it everywhere.