|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | +using Newtonsoft.Json; |
| 5 | + |
| 6 | +namespace Nest |
| 7 | +{ |
| 8 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 9 | + [ContractJsonConverter(typeof(AggregationJsonConverter<AutoDateHistogramAggregation>))] |
| 10 | + public interface IAutoDateHistogramAggregation : IBucketAggregation |
| 11 | + { |
| 12 | + [JsonProperty("field")] |
| 13 | + Field Field { get; set; } |
| 14 | + |
| 15 | + [JsonProperty("format")] |
| 16 | + string Format { get; set; } |
| 17 | + |
| 18 | + [JsonProperty("missing")] |
| 19 | + DateTime? Missing { get; set; } |
| 20 | + |
| 21 | + [JsonProperty("offset")] |
| 22 | + string Offset { get; set; } |
| 23 | + |
| 24 | + [JsonProperty("params")] |
| 25 | + IDictionary<string, object> Params { get; set; } |
| 26 | + |
| 27 | + [JsonProperty("script")] |
| 28 | + IScript Script { get; set; } |
| 29 | + |
| 30 | + [JsonProperty("time_zone")] |
| 31 | + string TimeZone { get; set; } |
| 32 | + } |
| 33 | + |
| 34 | + public class AutoDateHistogramAggregation : BucketAggregationBase, IAutoDateHistogramAggregation |
| 35 | + { |
| 36 | + private string _format; |
| 37 | + |
| 38 | + internal AutoDateHistogramAggregation() { } |
| 39 | + |
| 40 | + public AutoDateHistogramAggregation(string name) : base(name) { } |
| 41 | + |
| 42 | + public Field Field { get; set; } |
| 43 | + |
| 44 | + //see: https://github.com/elastic/elasticsearch/issues/9725 |
| 45 | + public string Format |
| 46 | + { |
| 47 | + get => !string.IsNullOrEmpty(_format) && |
| 48 | + !_format.Contains("date_optional_time") && |
| 49 | + (Missing.HasValue) |
| 50 | + ? _format + "||date_optional_time" |
| 51 | + : _format; |
| 52 | + set => _format = value; |
| 53 | + } |
| 54 | + |
| 55 | + public DateTime? Missing { get; set; } |
| 56 | + public string Offset { get; set; } |
| 57 | + public IDictionary<string, object> Params { get; set; } |
| 58 | + public IScript Script { get; set; } |
| 59 | + public string TimeZone { get; set; } |
| 60 | + |
| 61 | + internal override void WrapInContainer(AggregationContainer c) => c.AutoDateHistogram = this; |
| 62 | + } |
| 63 | + |
| 64 | + public class AutoDateHistogramAggregationDescriptor<T> |
| 65 | + : BucketAggregationDescriptorBase<AutoDateHistogramAggregationDescriptor<T>, IAutoDateHistogramAggregation, T> |
| 66 | + , IAutoDateHistogramAggregation |
| 67 | + where T : class |
| 68 | + { |
| 69 | + private string _format; |
| 70 | + |
| 71 | + Field IAutoDateHistogramAggregation.Field { get; set; } |
| 72 | + |
| 73 | + //see: https://github.com/elastic/elasticsearch/issues/9725 |
| 74 | + string IAutoDateHistogramAggregation.Format |
| 75 | + { |
| 76 | + get => !string.IsNullOrEmpty(_format) && |
| 77 | + !_format.Contains("date_optional_time") && |
| 78 | + (Self.Missing.HasValue) |
| 79 | + ? _format + "||date_optional_time" |
| 80 | + : _format; |
| 81 | + set => _format = value; |
| 82 | + } |
| 83 | + |
| 84 | + DateTime? IAutoDateHistogramAggregation.Missing { get; set; } |
| 85 | + |
| 86 | + string IAutoDateHistogramAggregation.Offset { get; set; } |
| 87 | + |
| 88 | + IDictionary<string, object> IAutoDateHistogramAggregation.Params { get; set; } |
| 89 | + |
| 90 | + IScript IAutoDateHistogramAggregation.Script { get; set; } |
| 91 | + |
| 92 | + string IAutoDateHistogramAggregation.TimeZone { get; set; } |
| 93 | + |
| 94 | + public AutoDateHistogramAggregationDescriptor<T> Field(Field field) => Assign(a => a.Field = field); |
| 95 | + |
| 96 | + public AutoDateHistogramAggregationDescriptor<T> Field(Expression<Func<T, object>> field) => Assign(a => a.Field = field); |
| 97 | + |
| 98 | + public AutoDateHistogramAggregationDescriptor<T> Script(string script) => Assign(a => a.Script = (InlineScript)script); |
| 99 | + |
| 100 | + public AutoDateHistogramAggregationDescriptor<T> Script(Func<ScriptDescriptor, IScript> scriptSelector) => |
| 101 | + Assign(a => a.Script = scriptSelector?.Invoke(new ScriptDescriptor())); |
| 102 | + |
| 103 | + public AutoDateHistogramAggregationDescriptor<T> Format(string format) => Assign(a => a.Format = format); |
| 104 | + |
| 105 | + public AutoDateHistogramAggregationDescriptor<T> TimeZone(string timeZone) => Assign(a => a.TimeZone = timeZone); |
| 106 | + |
| 107 | + public AutoDateHistogramAggregationDescriptor<T> Offset(string offset) => Assign(a => a.Offset = offset); |
| 108 | + |
| 109 | + public AutoDateHistogramAggregationDescriptor<T> Missing(DateTime? missing) => Assign(a => a.Missing = missing); |
| 110 | + } |
| 111 | +} |
0 commit comments