<commit_before>package com.sksamuel.elastic4s.searches.aggs

import com.sksamuel.elastic4s.ScriptBuilder
import com.sksamuel.exts.OptionImplicits._
import com.sksamuel.elastic4s.script.ScriptDefinition
import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude
import org.elasticsearch.search.aggregations.bucket.terms.{Terms, TermsAggregationBuilder, TermsAggregator}
import org.elasticsearch.search.aggregations.support.ValueType
import org.elasticsearch.search.aggregations.{AggregationBuilders, Aggregator}

case class TermsAggregationDefinition(name: String) extends AggregationDefinition {

  type B = TermsAggregationBuilder
  val builder: B = AggregationBuilders.terms(name)

  def field(field: String) = {
    builder.field(field)
    this
  }

  def script(script: ScriptDefinition) = {
    builder.script(ScriptBuilder(script))
    this
  }

  def missing(missing: String) = {
    builder.missing(missing)
    this
  }

  def size(size: Int): TermsAggregationDefinition = {
    builder.size(size)
    this
  }

  def minDocCount(minDocCount: Int): this.type = {
    builder.minDocCount(minDocCount)
    this
  }

  def showTermDocCountError(showTermDocCountError: Boolean): this.type = {
    builder.showTermDocCountError(showTermDocCountError)
    this
  }

  def valueType(valueType: ValueType): this.type = {
    builder.valueType(valueType)
    this
  }

  def executionHint(executionHint: String): this.type = {
    builder.executionHint(executionHint)
    this
  }

  def shardMinDocCount(shardMinDocCount: Long): this.type = {
    builder.shardMinDocCount(shardMinDocCount)
    this
  }

  def collectMode(collectMode: Aggregator.SubAggCollectionMode): this.type = {
    builder.collectMode(collectMode)
    this
  }

  def bucketCountThresholds(bucketCountThresholds: TermsAggregator.BucketCountThresholds): this.type = {
    builder.bucketCountThresholds(bucketCountThresholds)
    this
  }

  def order(order: Terms.Order): TermsAggregationDefinition = {
    builder.order(order)
    this
  }

  def shardSize(shardSize: Int): TermsAggregationDefinition = {
    builder.shardSize(shardSize)
    this
  }

  def includeExclude(include: String, exclude: String): TermsAggregationDefinition = {
    builder.includeExclude(new IncludeExclude(include.some.orNull, exclude.some.orNull))
    this
  }

  def includeExclude(include: Iterable[String], exclude: Iterable[String]): TermsAggregationDefinition = {
    // empty array doesn't work, has to be null
    val inc = if (include.isEmpty) null else include.toArray
    val exc = if (exclude.isEmpty) null else exclude.toArray
    builder.includeExclude(new IncludeExclude(inc, exc))
    this
  }
}<commit_msg>Provide option to include exclude array of long / double values apart from arrays of strings in TermsAggregationDefinition<commit_after>package com.sksamuel.elastic4s.searches.aggs

import com.sksamuel.elastic4s.ScriptBuilder
import com.sksamuel.exts.OptionImplicits._
import com.sksamuel.elastic4s.script.ScriptDefinition
import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude
import org.elasticsearch.search.aggregations.bucket.terms.{Terms, TermsAggregationBuilder, TermsAggregator}
import org.elasticsearch.search.aggregations.support.ValueType
import org.elasticsearch.search.aggregations.{AggregationBuilders, Aggregator}

case class TermsAggregationDefinition(name: String) extends AggregationDefinition {

  type B = TermsAggregationBuilder
  val builder: B = AggregationBuilders.terms(name)

  def field(field: String) = {
    builder.field(field)
    this
  }

  def script(script: ScriptDefinition) = {
    builder.script(ScriptBuilder(script))
    this
  }

  def missing(missing: String) = {
    builder.missing(missing)
    this
  }

  def size(size: Int): TermsAggregationDefinition = {
    builder.size(size)
    this
  }

  def minDocCount(minDocCount: Int): this.type = {
    builder.minDocCount(minDocCount)
    this
  }

  def showTermDocCountError(showTermDocCountError: Boolean): this.type = {
    builder.showTermDocCountError(showTermDocCountError)
    this
  }

  def valueType(valueType: ValueType): this.type = {
    builder.valueType(valueType)
    this
  }

  def executionHint(executionHint: String): this.type = {
    builder.executionHint(executionHint)
    this
  }

  def shardMinDocCount(shardMinDocCount: Long): this.type = {
    builder.shardMinDocCount(shardMinDocCount)
    this
  }

  def collectMode(collectMode: Aggregator.SubAggCollectionMode): this.type = {
    builder.collectMode(collectMode)
    this
  }

  def bucketCountThresholds(bucketCountThresholds: TermsAggregator.BucketCountThresholds): this.type = {
    builder.bucketCountThresholds(bucketCountThresholds)
    this
  }

  def order(order: Terms.Order): TermsAggregationDefinition = {
    builder.order(order)
    this
  }

  def shardSize(shardSize: Int): TermsAggregationDefinition = {
    builder.shardSize(shardSize)
    this
  }

  def includeExclude(include: String, exclude: String): TermsAggregationDefinition = {
    builder.includeExclude(new IncludeExclude(include.some.orNull, exclude.some.orNull))
    this
  }

  def includeExclude(include: Iterable[String], exclude: Iterable[String]): TermsAggregationDefinition = {
    // empty array doesn't work, has to be null
    val inc = if (include.isEmpty) null else include.toArray
    val exc = if (exclude.isEmpty) null else exclude.toArray
    builder.includeExclude(new IncludeExclude(inc, exc))
    this
  }

  def includeExclude(include: Iterable[Long], exclude: Iterable[Long]): TermsAggregationDefinition = {
    // empty array doesn't work, has to be null
    val inc = if (include.isEmpty) null else include.toArray
    val exc = if (exclude.isEmpty) null else exclude.toArray
    builder.includeExclude(new IncludeExclude(inc, exc))
    this
  }

  def includeExclude(include: Iterable[Double], exclude: Iterable[Double]): TermsAggregationDefinition = {
    // empty array doesn't work, has to be null
    val inc = if (include.isEmpty) null else include.toArray
    val exc = if (exclude.isEmpty) null else exclude.toArray
    builder.includeExclude(new IncludeExclude(inc, exc))
    this
  }
}
{'fraction_non_alphanumeric': 0.09117314829756333, 'fraction_numerical': 0.0009682104243989027, 'mean_word_length': 4.470432480141218, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5456986', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1840, 'n_words': 378}