/* 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;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
[InterfaceDataContract]
[ReadAs(typeof(DateHistogramAggregation))]
public interface IDateHistogramAggregation : IBucketAggregation
{
///
/// Extend the bounds of the date histogram beyond the data itself,
/// forcing the aggregation to start building buckets on
/// a specific min and/or max value.
/// Using extended bounds only makes sense when is 0
/// as empty buckets will never be returned if it is greater than 0.
///
[DataMember(Name ="extended_bounds")]
ExtendedBounds ExtendedBounds { get; set; }
///
/// The hard_bounds is a counterpart of extended_bounds and can limit the range of buckets in the histogram.
/// It is particularly useful in the case of open data ranges that can result in a very large number of buckets.
///
[DataMember(Name = "hard_bounds")]
HardBounds HardBounds { get; set; }
///
/// The field to target
///
[DataMember(Name ="field")]
Field Field { get; set; }
///
/// Return a formatted date string as the key instead an epoch long
///
[DataMember(Name ="format")]
string Format { get; set; }
///
/// The calendar interval to use when bucketing documents
///
[DataMember(Name ="calendar_interval")]
Union CalendarInterval { get; set; }
///
/// The fixed interval to use when bucketing documents
///
[DataMember(Name ="fixed_interval")]
Time FixedInterval { get; set; }
///
/// The minimum number of documents that a bucket must contain to be returned in the response.
/// The default is 0 meaning that buckets with no documents will be returned.
///
[DataMember(Name ="min_doc_count")]
int? MinimumDocumentCount { get; set; }
///
/// Defines how to treat documents that are missing a value. By default, they are ignored,
/// but it is also possible to treat them as if they have a value.
///
[DataMember(Name ="missing")]
DateTime? Missing { get; set; }
///
/// Change the start value of each bucket by the specified positive (+) or negative offset (-) duration,
/// such as 1h for an hour, or 1d for a day.
///
[DataMember(Name ="offset")]
string Offset { get; set; }
///
/// Defines an order in which returned buckets are sorted.
/// By default the returned buckets are sorted by their key ascending.
///
[DataMember(Name ="order")]
HistogramOrder Order { get; set; }
///
[DataMember(Name ="script")]
IScript Script { get; set; }
///
/// Used to indicate that bucketing should use a different time zone.
/// Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or -08:00)
/// or as a timezone id, an identifier used in the TZ database like America/Los_Angeles.
///
[DataMember(Name ="time_zone")]
string TimeZone { get; set; }
}
public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
{
private string _format;
internal DateHistogramAggregation() { }
public DateHistogramAggregation(string name) : base(name) { }
///
public ExtendedBounds ExtendedBounds { get; set; }
///
public HardBounds HardBounds { get; set; }
///
public Field Field { get; set; }
///
public string Format
{
get => !string.IsNullOrEmpty(_format) &&
!_format.Contains("date_optional_time") &&
(ExtendedBounds != null || HardBounds != null || Missing.HasValue)
? _format + "||date_optional_time"
: _format;
set => _format = value;
}
///
public Union CalendarInterval { get; set; }
///
public Time FixedInterval { get; set; }
///
public int? MinimumDocumentCount { get; set; }
///
public DateTime? Missing { get; set; }
///
public string Offset { get; set; }
///
public HistogramOrder Order { get; set; }
///
public IScript Script { get; set; }
///
public string TimeZone { get; set; }
internal override void WrapInContainer(AggregationContainer c) => c.DateHistogram = this;
}
public class DateHistogramAggregationDescriptor
: BucketAggregationDescriptorBase, IDateHistogramAggregation, T>
, IDateHistogramAggregation
where T : class
{
private string _format;
ExtendedBounds IDateHistogramAggregation.ExtendedBounds { get; set; }
HardBounds IDateHistogramAggregation.HardBounds { get; set; }
Field IDateHistogramAggregation.Field { get; set; }
//see: https://github.com/elastic/elasticsearch/issues/9725
string IDateHistogramAggregation.Format
{
get => !string.IsNullOrEmpty(_format) &&
!_format.Contains("date_optional_time") &&
(Self.ExtendedBounds != null || Self.HardBounds != null || Self.Missing.HasValue)
? _format + "||date_optional_time"
: _format;
set => _format = value;
}
Union IDateHistogramAggregation.CalendarInterval { get; set; }
Time IDateHistogramAggregation.FixedInterval { get; set; }
int? IDateHistogramAggregation.MinimumDocumentCount { get; set; }
DateTime? IDateHistogramAggregation.Missing { get; set; }
string IDateHistogramAggregation.Offset { get; set; }
HistogramOrder IDateHistogramAggregation.Order { get; set; }
IScript IDateHistogramAggregation.Script { get; set; }
string IDateHistogramAggregation.TimeZone { get; set; }
///
public DateHistogramAggregationDescriptor Field(Field field) => Assign(field, (a, v) => a.Field = v);
///
public DateHistogramAggregationDescriptor Field(Expression> field) => Assign(field, (a, v) => a.Field = v);
///
public DateHistogramAggregationDescriptor Script(string script) => Assign((InlineScript)script, (a, v) => a.Script = v);
///
public DateHistogramAggregationDescriptor Script(Func scriptSelector) =>
Assign(scriptSelector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor()));
///
public DateHistogramAggregationDescriptor CalendarInterval(DateMathTime interval) => Assign(interval, (a, v) => a.CalendarInterval = v);
///
public DateHistogramAggregationDescriptor CalendarInterval(DateInterval? interval) => Assign(interval, (a, v) => a.CalendarInterval = v);
///
public DateHistogramAggregationDescriptor FixedInterval(Time interval) => Assign(interval, (a, v) => a.FixedInterval = v);
///
public DateHistogramAggregationDescriptor Format(string format) => Assign(format, (a, v) => a.Format = v);
///
public DateHistogramAggregationDescriptor MinimumDocumentCount(int? minimumDocumentCount) =>
Assign(minimumDocumentCount, (a, v) => a.MinimumDocumentCount = v);
///
public DateHistogramAggregationDescriptor TimeZone(string timeZone) => Assign(timeZone, (a, v) => a.TimeZone = v);
///
public DateHistogramAggregationDescriptor Offset(string offset) => Assign(offset, (a, v) => a.Offset = v);
///
public DateHistogramAggregationDescriptor Order(HistogramOrder order) => Assign(order, (a, v) => a.Order = v);
///
public DateHistogramAggregationDescriptor OrderAscending(string key) =>
Assign(new HistogramOrder { Key = key, Order = SortOrder.Descending }, (a, v) => a.Order = v);
///
public DateHistogramAggregationDescriptor OrderDescending(string key) =>
Assign(new HistogramOrder { Key = key, Order = SortOrder.Descending }, (a, v) => a.Order = v);
///
public DateHistogramAggregationDescriptor ExtendedBounds(DateMath min, DateMath max) =>
Assign(new ExtendedBounds { Minimum = min, Maximum = max }, (a, v) => a.ExtendedBounds = v);
///
public DateHistogramAggregationDescriptor HardBounds(DateMath min, DateMath max) =>
Assign(new HardBounds { Minimum = min, Maximum = max }, (a, v) => a.HardBounds = v);
///
public DateHistogramAggregationDescriptor Missing(DateTime? missing) => Assign(missing, (a, v) => a.Missing = v);
}
}