<commit_before>package com.sksamuel.elastic4s.http.search.aggs

import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}
import com.sksamuel.elastic4s.searches.DateHistogramInterval
import com.sksamuel.elastic4s.searches.aggs._
import com.sksamuel.elastic4s.searches.aggs.pipeline._
import com.sksamuel.elastic4s.http.search.aggs.pipeline.BucketSelectorPipelineBuilder

object AggregationBuilderFn {
  def apply(agg: AbstractAggregation): XContentBuilder = {
    val builder = agg match {

      case agg: AvgAggregationDefinition => AvgAggregationBuilder(agg)
      case agg: CardinalityAggregationDefinition => CardinalityAggregationBuilder(agg)
      case agg: ChildrenAggregationDefinition => ChildrenAggregationBuilder(agg)
      case agg: DateHistogramAggregation => DateHistogramAggregationBuilder(agg)
      case agg: ExtendedStatsAggregationDefinition => ExtendedStatsAggregationBuilder(agg)
      case agg: FilterAggregationDefinition => FilterAggregationBuilder(agg)
      case agg: FiltersAggregationDefinition => FiltersAggregationBuilder(agg)
      case agg: KeyedFiltersAggregationDefinition => KeyedFiltersAggregationBuilder(agg)
      case agg: GeoBoundsAggregationDefinition => GeoBoundsAggregationBuilder(agg)
      case agg: GeoHashGridAggregationDefinition => GeoHashGridAggregationBuilder(agg)
      case agg: MaxAggregationDefinition => MaxAggregationBuilder(agg)
      case agg: MinAggregationDefinition => MinAggregationBuilder(agg)
      case agg: MissingAggregationDefinition => MissingAggregationBuilder(agg)
      case agg: NestedAggregationDefinition => NestedAggregationBuilder(agg)
      case agg: PercentilesAggregationDefinition => PercentilesAggregationBuilder(agg)
      case agg: SamplerAggregationDefinition => SamplerAggregationBuilder(agg)
      case agg: StatsAggregationDefinition => StatsAggregationBuilder(agg)
      case agg: SumAggregationDefinition => SumAggregationBuilder(agg)
      case agg: TopHitsAggregationDefinition => TopHitsAggregationBuilder(agg)
      case agg: TermsAggregationDefinition => TermsAggregationBuilder(agg)
      case agg: ValueCountAggregationDefinition => ValueCountAggregationBuilder(agg)

      case agg: RangeAggregationDefinition => RangeAggregationBuilder(agg)
      case agg: DateRangeAggregation => DateRangeAggregationBuilder(agg)

      // pipeline aggs
      case agg: BucketSelectorDefinition => BucketSelectorPipelineBuilder(agg)
      case agg: DerivativeDefinition => DerivativePipelineAggBuilder(agg)
      case agg: MaxBucketDefinition => MaxBucketPipelineAggBuilder(agg)
      case agg: SumBucketDefinition => SumBucketPipelineAggBuilder(agg)
      case agg: BucketScriptDefinition => BucketScriptPipelineAggBuilder(agg)
      case agg: CumulativeSumDefinition => CumulativeSumAggBuilder(agg)

      // Not implemented
      case ni => throw new NotImplementedError(s"Aggregation ${ni.getClass.getName} has not yet been implemented for the HTTP client.")
    }
    builder
  }
}

object DerivativePipelineAggBuilder {
  def apply(agg: DerivativeDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("derivative")
    builder.field("buckets_path", agg.bucketsPath)
    agg.unit.map(_.toSeconds).map(DateHistogramInterval.seconds).foreach(i=>builder.field("unit", i.interval))
    agg.gapPolicy.foreach(policy=> builder.field("gap_policy", policy.toString.toLowerCase))
    agg.format.foreach(f=>builder.field("format", f))
    builder.endObject()
    AggMetaDataFn(agg, builder)
    builder.endObject()
  }
}


object CumulativeSumAggBuilder {
  def apply(agg: CumulativeSumDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("cumulative_sum")
    builder.field("buckets_path", agg.bucketsPath)
    agg.format.foreach(f=>builder.field("format", f))
    builder.endObject()
    AggMetaDataFn(agg, builder)
    builder.endObject()
  }
}

object MaxBucketPipelineAggBuilder {
  def apply(agg: MaxBucketDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("max_bucket")
    builder.field("buckets_path", agg.bucketsPath)
    builder.endObject().endObject()
  }
}

object SumBucketPipelineAggBuilder {
  def apply(agg: SumBucketDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("sum_bucket")
    builder.field("buckets_path", agg.bucketsPath)
    builder.endObject().endObject()<commit_msg>small naming fix<commit_after>package com.sksamuel.elastic4s.http.search.aggs

import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}
import com.sksamuel.elastic4s.searches.DateHistogramInterval
import com.sksamuel.elastic4s.searches.aggs._
import com.sksamuel.elastic4s.searches.aggs.pipeline._
import com.sksamuel.elastic4s.http.search.aggs.pipeline.BucketSelectorPipelineBuilder

object AggregationBuilderFn {
  def apply(agg: AbstractAggregation): XContentBuilder = {
    val builder = agg match {

      case agg: AvgAggregationDefinition => AvgAggregationBuilder(agg)
      case agg: CardinalityAggregationDefinition => CardinalityAggregationBuilder(agg)
      case agg: ChildrenAggregationDefinition => ChildrenAggregationBuilder(agg)
      case agg: DateHistogramAggregation => DateHistogramAggregationBuilder(agg)
      case agg: ExtendedStatsAggregationDefinition => ExtendedStatsAggregationBuilder(agg)
      case agg: FilterAggregationDefinition => FilterAggregationBuilder(agg)
      case agg: FiltersAggregationDefinition => FiltersAggregationBuilder(agg)
      case agg: KeyedFiltersAggregationDefinition => KeyedFiltersAggregationBuilder(agg)
      case agg: GeoBoundsAggregationDefinition => GeoBoundsAggregationBuilder(agg)
      case agg: GeoHashGridAggregationDefinition => GeoHashGridAggregationBuilder(agg)
      case agg: MaxAggregationDefinition => MaxAggregationBuilder(agg)
      case agg: MinAggregationDefinition => MinAggregationBuilder(agg)
      case agg: MissingAggregationDefinition => MissingAggregationBuilder(agg)
      case agg: NestedAggregationDefinition => NestedAggregationBuilder(agg)
      case agg: PercentilesAggregationDefinition => PercentilesAggregationBuilder(agg)
      case agg: SamplerAggregationDefinition => SamplerAggregationBuilder(agg)
      case agg: StatsAggregationDefinition => StatsAggregationBuilder(agg)
      case agg: SumAggregationDefinition => SumAggregationBuilder(agg)
      case agg: TopHitsAggregationDefinition => TopHitsAggregationBuilder(agg)
      case agg: TermsAggregationDefinition => TermsAggregationBuilder(agg)
      case agg: ValueCountAggregationDefinition => ValueCountAggregationBuilder(agg)

      case agg: RangeAggregationDefinition => RangeAggregationBuilder(agg)
      case agg: DateRangeAggregation => DateRangeAggregationBuilder(agg)

      // pipeline aggs
      case agg: BucketSelectorDefinition => BucketSelectorPipelineBuilder(agg)
      case agg: DerivativeDefinition => DerivativePipelineAggBuilder(agg)
      case agg: MaxBucketDefinition => MaxBucketPipelineAggBuilder(agg)
      case agg: SumBucketDefinition => SumBucketPipelineAggBuilder(agg)
      case agg: BucketScriptDefinition => BucketScriptPipelineAggBuilder(agg)
      case agg: CumulativeSumDefinition => CumulativeSumPipelineAggBuilder(agg)

      // Not implemented
      case ni => throw new NotImplementedError(s"Aggregation ${ni.getClass.getName} has not yet been implemented for the HTTP client.")
    }
    builder
  }
}

object DerivativePipelineAggBuilder {
  def apply(agg: DerivativeDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("derivative")
    builder.field("buckets_path", agg.bucketsPath)
    agg.unit.map(_.toSeconds).map(DateHistogramInterval.seconds).foreach(i=>builder.field("unit", i.interval))
    agg.gapPolicy.foreach(policy=> builder.field("gap_policy", policy.toString.toLowerCase))
    agg.format.foreach(f=>builder.field("format", f))
    builder.endObject()
    AggMetaDataFn(agg, builder)
    builder.endObject()
  }
}


object CumulativeSumPipelineAggBuilder {
  def apply(agg: CumulativeSumDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("cumulative_sum")
    builder.field("buckets_path", agg.bucketsPath)
    agg.format.foreach(f=>builder.field("format", f))
    builder.endObject()
    AggMetaDataFn(agg, builder)
    builder.endObject()
  }
}

object MaxBucketPipelineAggBuilder {
  def apply(agg: MaxBucketDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("max_bucket")
    builder.field("buckets_path", agg.bucketsPath)
    builder.endObject().endObject()
  }
}

object SumBucketPipelineAggBuilder {
  def apply(agg: SumBucketDefinition): XContentBuilder = {
    val builder = XContentFactory.jsonBuilder().startObject("sum_bucket")
    builder.field("buckets_path", agg.bucketsPath)
    builder.endObject().endObject()
{'fraction_non_alphanumeric': 0.0889835257200493, 'fraction_numerical': 0.0013448391796481004, 'mean_word_length': 5.576271186440678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '145470', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2394, 'n_words': 445}