/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.Serialization; namespace OpenSearch.Client { /// /// An aggregation that computes metrics based on values extracted in /// one way or another from the documents that are being aggregated. /// public interface IMetricAggregation : IAggregation { /// /// The field on which to aggregate /// [DataMember(Name ="field")] Field Field { get; set; } /// /// The value to use when the aggregation finds a missing value in a document /// [DataMember(Name ="missing")] double? Missing { get; set; } /// /// The script to use for the aggregation /// [DataMember(Name ="script")] IScript Script { get; set; } } /// /// A metric aggregation that can accept a format to use for the output of the aggregation /// public interface IFormattableMetricAggregation : IMetricAggregation { /// /// The format to use for the output of the aggregation /// [DataMember(Name = "format")] string Format { get; set; } } public abstract class MetricAggregationBase : AggregationBase, IMetricAggregation { internal MetricAggregationBase() { } protected MetricAggregationBase(string name, Field field) : base(name) => Field = field; public Field Field { get; set; } public double? Missing { get; set; } public virtual IScript Script { get; set; } } public abstract class FormattableMetricAggregationBase : MetricAggregationBase, IFormattableMetricAggregation { internal FormattableMetricAggregationBase() { } protected FormattableMetricAggregationBase(string name, Field field) : base(name, field) { } /// public string Format { get; set; } } public abstract class MetricAggregationDescriptorBase : DescriptorBase, IMetricAggregation where TMetricAggregation : MetricAggregationDescriptorBase , TMetricAggregationInterface, IMetricAggregation where T : class where TMetricAggregationInterface : class, IMetricAggregation { Field IMetricAggregation.Field { get; set; } IDictionary IAggregation.Meta { get; set; } double? IMetricAggregation.Missing { get; set; } string IAggregation.Name { get; set; } IScript IMetricAggregation.Script { get; set; } /// public TMetricAggregation Field(Field field) => Assign(field, (a, v) => a.Field = v); /// public TMetricAggregation Field(Expression> field) => Assign(field, (a, v) => a.Field = v); /// public virtual TMetricAggregation Script(string script) => Assign((InlineScript)script, (a, v) => a.Script = v); /// public virtual TMetricAggregation Script(Func scriptSelector) => Assign(scriptSelector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor())); /// public TMetricAggregation Missing(double? missing) => Assign(missing, (a, v) => a.Missing = v); /// public TMetricAggregation Meta(Func, FluentDictionary> selector) => Assign(selector, (a, v) => a.Meta = v?.Invoke(new FluentDictionary())); } public abstract class FormattableMetricAggregationDescriptorBase : MetricAggregationDescriptorBase, IFormattableMetricAggregation where TFormattableMetricAggregation : FormattableMetricAggregationDescriptorBase , TFormattableMetricAggregationInterface, IFormattableMetricAggregation where T : class where TFormattableMetricAggregationInterface : class, IFormattableMetricAggregation { string IFormattableMetricAggregation.Format { get; set; } /// public TFormattableMetricAggregation Format(string format) => Assign(format, (a, v) => a.Format = v); } }