
import Foundation

/**
 A single-value metrics aggregation that computes the average of numeric values
 that are extracted from the aggregated documents. These values can be extracted
 either from specific numeric fields in the documents, or be generated by a
 provided script.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html)
 */
public struct AvgAggregation: Aggregation {
    public var aggs: [Aggregation]?
  
    /// :nodoc:
    public static var typeKey = AggregationResponseMap.avg
    
    /// :nodoc:
    public var name: String
    
    /// :nodoc:
    public let field: String?
    
    /// :nodoc:
    public let script: Script?
    
    /// :nodoc:
    public let missing: Int?
    
    enum CodingKeys: String, CodingKey {
        case field
        case script
        case missing
        case aggs
    }
    
    /// Create an [avg](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(name: String,
                field: String? = nil,
                script: Script? = nil,
                missing: Int? = nil,
                aggs: [Aggregation]? = nil
                ) {
        
        self.name = name
        self.field = field
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }
    
    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A single-value metrics aggregation that calculates an approximate count of
 distinct values. Values can be extracted either from specific fields in the
 document or generated by a script.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html)
 */
public struct CardinalityAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.cardinality

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let precisionThreshold: Int?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: String?

    enum CodingKeys: String, CodingKey {
        case field
        case precisionThreshold = "precision_threshold"
        case script
        case missing
        case aggs
    }

    /// Create a [cardinality](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - precisionThreshold: Allows to trade memory for accuracy, and defines a unique count below which counts are expected to be close to accurate
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        precisionThreshold: Int? = nil,
        script: Script? = nil,
        missing: String? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.precisionThreshold = precisionThreshold
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(precisionThreshold, forKey: .precisionThreshold)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A multi-value metrics aggregation that computes stats over numeric values
 extracted from the aggregated documents. These values can be extracted either
 from specific numeric fields in the documents, or be generated by a provided
 script.

 The extended_stats aggregations is an extended version of the stats
 aggregation, where additional metrics are added such as sum_of_squares,
 variance, std_deviation and std_deviation_bounds.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html)
 */
public struct ExtendedStatsAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.extendedStats

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let sigma: Int?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: Int?

    enum CodingKeys: String, CodingKey {
        case field
        case sigma
        case script
        case missing
        case aggs
    }

    /// Create an [extended_stats](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - sigma: Controls how many standard deviations +/- from the mean should be returned
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        sigma: Int? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.sigma = sigma
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(sigma, forKey: .sigma)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A metric aggregation that computes the bounding box containing all geo_point values for a field.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html)
 */
public struct GeoBoundsAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.geoBounds

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let wrapLongitude: Bool?

    enum CodingKeys: String, CodingKey {
        case field
        case wrapLongitude = "wrap_longitude"
        case aggs
    }

    /// Creates a [geo_bounds](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - wrapLongitude: Specifies whether the bounding box should be allowed to overlap the international date line
    public init(name: String,
                field: String? = nil,
                wrapLongitude: Bool? = nil,
                aggs: [Aggregation]? = nil
      ) {
        self.name = name
        self.field = field
        self.wrapLongitude = wrapLongitude
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(wrapLongitude, forKey: .wrapLongitude)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A metric aggregation that computes the weighted centroid from all coordinate
 values for a Geo-point datatype field.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html)
 */
public struct GeoCentroidAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.geoCentroid

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    enum CodingKeys: String, CodingKey {
        case field
        case aggs
    }

    /// Creates a [geo_point](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
  public init(name: String, field: String, aggs: [Aggregation]? = nil) {
        self.name = name
        self.field = field
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encode(field, forKey: .field)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A single-value metrics aggregation that keeps track and returns the maximum
 value among the numeric values extracted from the aggregated documents. These
 values can be extracted either from specific numeric fields in the documents,
 or be generated by a provided script.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html)
 */
public struct MaxAggregation: Aggregation {

    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.max

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: Int?

    enum CodingKeys: String, CodingKey {
        case field
        case script
        case missing
        case aggs
    }

    /// Creates a [max](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A single-value metrics aggregation that keeps track and returns the minimum
 value among numeric values extracted from the aggregated documents. These
 values can be extracted either from specific numeric fields in the documents,
 or be generated by a provided script.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html)
 */
public struct MinAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.min

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: Int?

    enum CodingKeys: String, CodingKey {
        case field
        case script
        case missing
        case aggs
    }

    /// Creates a [min](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A multi-value metrics aggregation that computes stats over numeric values
 extracted from the aggregated documents. These values can be extracted either
 from specific numeric fields in the documents, or be generated by a provided
 script.

 The stats that are returned consist of: min, max, sum, count and avg.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html)
 */
public struct StatsAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.stats

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: Int?

    enum CodingKeys: String, CodingKey {
        case field
        case script
        case missing
        case aggs
    }

    /// Creates a [stats](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A single-value metrics aggregation that sums up numeric values that are
 extracted from the aggregated documents. These values can be extracted either
 from specific numeric fields in the documents, or be generated by a provided
 script.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html)
 */
public struct SumAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.sum

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let script: Script?

    /// :nodoc:
    public let missing: Int?

    enum CodingKeys: String, CodingKey {
        case field
        case script
        case missing
        case aggs
    }

    /// Creates a [sum](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.script = script
        self.missing = missing
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        try valuesContainer.encodeIfPresent(missing, forKey: .missing)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A top_hits metric aggregator keeps track of the most relevant document being
 aggregated. This aggregator is intended to be used as a sub aggregator, so that
 the top matching documents can be aggregated per bucket.

 The top_hits aggregator can effectively be used to group result sets by certain
 fields via a bucket aggregator. One or more bucket aggregators determines by
 which properties a result set get sliced into.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)
 */
public struct TopHitsAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.topHits

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let from: Int?

    /// :nodoc:
    public let size: Int?

    enum CodingKeys: String, CodingKey {
        case name
        case from
        case size
        case aggs
    }

    /// Creates a [top_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - from: The offset from the first result you want to fetch
    ///   - size: The maximum number of top matching hits to return per bucket
  public init(name: String, from: Int? = nil, size: Int? = nil, aggs: [Aggregation]? = nil) {
        self.name = name
        self.from = from
        self.size = size
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(from, forKey: .from)
        try valuesContainer.encodeIfPresent(size, forKey: .size)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}

/**
 A single-value metrics aggregation that counts the number of values that are
 extracted from the aggregated documents. These values can be extracted either
 from specific fields in the documents, or be generated by a provided script.
 Typically, this aggregator will be used in conjunction with other single-value
 aggregations. For example, when computing the avg one might be interested in
 the number of values the average is computed over.

 [More information](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html)
 */
public struct ValueCountAggregation: Aggregation {
    public var aggs: [Aggregation]?

    /// :nodoc:
    public static var typeKey = AggregationResponseMap.valueCount

    /// :nodoc:
    public var name: String

    /// :nodoc:
    public let field: String?

    /// :nodoc:
    public let script: Script?

    enum CodingKeys: String, CodingKey {
        case field
        case script
        case aggs
    }

    /// Creates a [value_count](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html) aggregation
    ///
    /// - Parameters:
    ///   - name: The aggregation name
    ///   - field: The field to perform the aggregation over
    ///   - script: A script used to calculate the values
    ///   - missing: Defines how documents that are missing a value should be treated
    public init(
        name: String,
        field: String? = nil,
        script: Script? = nil,
        missing: Int? = nil,
        aggs: [Aggregation]? = nil
        ) {
        self.name = name
        self.field = field
        self.script = script
        self.aggs = aggs
    }

    /// :nodoc:
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)
        var valuesContainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: DynamicKey(stringValue: type(of: self).typeKey.rawValue)!)
        try valuesContainer.encodeIfPresent(field, forKey: .field)
        try valuesContainer.encodeIfPresent(script, forKey: .script)
        if aggs != nil {
          if aggs != nil {
          if aggs != nil && aggs!.count > 0 {
          var aggContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: DynamicKey(stringValue: "aggs")!)
          for agg in aggs! {
            try aggContainer.encode(AnyAggregation(agg), forKey: DynamicKey(stringValue: agg.name)!)
          }
        }
      }
        }
    }
}







