Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions src/Nest/DSL/Aggregations/AggregationDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Elasticsearch.Net;
using Newtonsoft.Json;

namespace Nest
{
public class AggregationDescriptor<T>
where T : class
{
internal readonly IDictionary<string, AggregationDescriptor<T>> _Aggregations =
new Dictionary<string, AggregationDescriptor<T>>();

[JsonProperty("aggs", Order = 100)]
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
internal IDictionary<string, AggregationDescriptor<T>> _NestedAggregations;


[JsonProperty("avg")]
internal AverageAggregationDescriptor<T> _Average { get; set; }
public AggregationDescriptor<T> Average(string name, Func<AverageAggregationDescriptor<T>, AverageAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Average = d);
}

[JsonProperty("date_histogram")]
internal DateHistogramAggregationDescriptor<T> _DateHistogram { get; set; }
public AggregationDescriptor<T> DateHistogram(string name,
Func<DateHistogramAggregationDescriptor<T>, DateHistogramAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._DateHistogram = d);
}

[JsonProperty("date_range")]
internal DateRangeAggregationDescriptor<T> _DateRange { get; set; }
public AggregationDescriptor<T> DateRange(string name,
Func<DateRangeAggregationDescriptor<T>, DateRangeAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._DateRange = d);
}

[JsonProperty("extended_stats")]
internal ExtendedStatsAggregationDescriptor<T> _ExtendedStats { get; set; }
public AggregationDescriptor<T> ExtendedStats(string name,
Func<ExtendedStatsAggregationDescriptor<T>, ExtendedStatsAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._ExtendedStats = d);
}

[JsonProperty("filter")]
internal FilterAggregationDescriptor<T> _Filter { get; set; }
public AggregationDescriptor<T> Filter(string name,
Func<FilterAggregationDescriptor<T>, FilterAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Filter = d);
}

[JsonProperty("geo_distance")]
internal GeoDistanceAggregationDescriptor<T> _GeoDistance { get; set; }
public AggregationDescriptor<T> GeoDistance(string name,
Func<GeoDistanceAggregationDescriptor<T>, GeoDistanceAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._GeoDistance = d);
}

[JsonProperty("geohash_grid")]
internal GeoHashAggregationDescriptor<T> _GeoHash { get; set; }
public AggregationDescriptor<T> GeoHash(string name,
Func<GeoHashAggregationDescriptor<T>, GeoHashAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._GeoHash = d);
}

[JsonProperty("histogram")]
internal HistogramAggregationDescriptor<T> _Histogram { get; set; }
public AggregationDescriptor<T> Histogram(string name,
Func<HistogramAggregationDescriptor<T>, HistogramAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Histogram = d);
}

[JsonProperty("global")]
internal GlobalAggregationDescriptor<T> _Global { get; set; }
public AggregationDescriptor<T> Global(string name,
Func<GlobalAggregationDescriptor<T>, GlobalAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Global = d);
}

[JsonProperty("ip_range")]
internal Ip4RangeAggregationDescriptor<T> _IpRange { get; set; }
public AggregationDescriptor<T> IpRange(string name,
Func<Ip4RangeAggregationDescriptor<T>, Ip4RangeAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._IpRange = d);
}

[JsonProperty("max")]
internal MaxAggregationDescriptor<T> _Max { get; set; }
public AggregationDescriptor<T> Max(string name, Func<MaxAggregationDescriptor<T>, MaxAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Max = d);
}

[JsonProperty("min")]
internal MinAggregationDescriptor<T> _Min { get; set; }
public AggregationDescriptor<T> Min(string name, Func<MinAggregationDescriptor<T>, MinAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Min = d);
}

[JsonProperty("missing")]
internal MissingAggregationDescriptor<T> _Missing { get; set; }
public AggregationDescriptor<T> Missing(string name, Func<MissingAggregationDescriptor<T>, MissingAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Missing = d);
}

[JsonProperty("nested")]
internal NestedAggregationDescriptor<T> _Nested { get; set; }
public AggregationDescriptor<T> Nested(string name, Func<NestedAggregationDescriptor<T>, NestedAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Nested = d);
}

[JsonProperty("range")]
internal RangeAggregationDescriptor<T> _Range { get; set; }
public AggregationDescriptor<T> Range(string name, Func<RangeAggregationDescriptor<T>, RangeAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Range = d);
}

[JsonProperty("stats")]
internal StatsAggregationDescriptor<T> _Stats { get; set; }
public AggregationDescriptor<T> Stats(string name, Func<StatsAggregationDescriptor<T>, StatsAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Stats = d);
}

[JsonProperty("sum")]
internal SumAggregationDescriptor<T> _Sum { get; set; }
public AggregationDescriptor<T> Sum(string name, Func<SumAggregationDescriptor<T>, SumAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Sum = d);
}

[JsonProperty("terms")]
internal TermsAggregationDescriptor<T> _Terms { get; set; }
public AggregationDescriptor<T> Terms(string name, Func<TermsAggregationDescriptor<T>, TermsAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._Terms = d);
}

[JsonProperty("value_count")]
internal ValueCountAggregationDescriptor<T> _ValueCount { get; set; }
public AggregationDescriptor<T> ValueCount(string name,
Func<ValueCountAggregationDescriptor<T>, ValueCountAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a._ValueCount = d);
}

private AggregationDescriptor<T> _SetInnerAggregation<TAggregation>(
string key,
Func<TAggregation, TAggregation> selector
, Action<AggregationDescriptor<T>, TAggregation> setter
)
where TAggregation : IAggregationDescriptor, new()

{
var innerDescriptor = selector(new TAggregation());
var descriptor = new AggregationDescriptor<T>();
setter(descriptor, innerDescriptor);
var bucket = innerDescriptor as IBucketAggregationDescriptor<T>;
if (bucket != null && bucket.NestedAggregations.HasAny())
{
descriptor._NestedAggregations = bucket.NestedAggregations;
}
this._Aggregations[key] = descriptor;
return this;

}


}
}
16 changes: 16 additions & 0 deletions src/Nest/DSL/Aggregations/AverageAggregationDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Nest.Resolvers;
using Newtonsoft.Json;

namespace Nest
{
public class AverageAggregationDescriptor<T> : MetricAggregationBaseDescriptor<AverageAggregationDescriptor<T>, T>
where T : class
{

}
}
31 changes: 31 additions & 0 deletions src/Nest/DSL/Aggregations/BucketAggregationBaseDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace Nest
{

public interface IBucketAggregationDescriptor<T>
where T : class
{

IDictionary<string, AggregationDescriptor<T>> NestedAggregations { get; set; }
}

public abstract class BucketAggregationBaseDescriptor<TBucketAggregation, T>
: IAggregationDescriptor, IBucketAggregationDescriptor<T>
where TBucketAggregation : BucketAggregationBaseDescriptor<TBucketAggregation, T>
where T : class
{
IDictionary<string, AggregationDescriptor<T>> IBucketAggregationDescriptor<T>.NestedAggregations { get; set; }

public TBucketAggregation Aggregations(Func<AggregationDescriptor<T>, AggregationDescriptor<T>> selector)
{
var aggs = selector(new AggregationDescriptor<T>());
if (aggs == null) return (TBucketAggregation)this;
((IBucketAggregationDescriptor<T>)this).NestedAggregations = aggs._Aggregations;
return (TBucketAggregation)this;
}
}
}
158 changes: 158 additions & 0 deletions src/Nest/DSL/Aggregations/DateHistogramAggregationDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Elasticsearch.Net;
using Nest.Resolvers;
using Nest.Resolvers.Converters;
using Newtonsoft.Json;

namespace Nest
{
public class DateHistogramAggregationDescriptor<T> : BucketAggregationBaseDescriptor<DateHistogramAggregationDescriptor<T>, T>
where T : class
{
[JsonProperty("field")]
internal PropertyPathMarker _Field { get; set; }

public DateHistogramAggregationDescriptor()
{
this.Format("yyyy-MM-dd");
}


public DateHistogramAggregationDescriptor<T> Field(string field)
{
this._Field = field;
return this;
}

public DateHistogramAggregationDescriptor<T> Field(Expression<Func<T, object>> field)
{
this._Field = field;
return this;
}

[JsonProperty("script")]
internal string _Script { get; set; }

public DateHistogramAggregationDescriptor<T> Script(string script)
{
this._Script = script;
return this;
}

[JsonProperty("params")]
internal FluentDictionary<string, object> _Params { get; set; }

public DateHistogramAggregationDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramSelector)
{
this._Params = paramSelector(new FluentDictionary<string, object>());
return this;
}

[JsonProperty("interval")]
internal string _Interval { get; set; }

public DateHistogramAggregationDescriptor<T> Interval(string interval)
{
this._Interval = interval;
return this;
}

[JsonProperty("format")]
internal string _Format { get; set; }

public DateHistogramAggregationDescriptor<T> Format(string format)
{
if (format.IsNullOrEmpty())
return this;
this._Format = format;
return this;
}

[JsonProperty("min_doc_count")]
internal int? _MinimumDocumentCount { get; set; }

public DateHistogramAggregationDescriptor<T> MinimumDocumentCount(int minimumDocumentCount)
{
this._MinimumDocumentCount = minimumDocumentCount;
return this;
}

[JsonProperty("pre_zone")]
internal string _PreZone { get; set; }

public DateHistogramAggregationDescriptor<T> PreZone(string preZone)
{
this._PreZone = preZone;
return this;
}

[JsonProperty("post_zone")]
internal string _PostZone { get; set; }

public DateHistogramAggregationDescriptor<T> PostZone(string postZone)
{
this._PostZone = postZone;
return this;
}

[JsonProperty("time_zone")]
internal string _TimeZone { get; set; }

public DateHistogramAggregationDescriptor<T> TimeZone(string timeZone)
{
this._TimeZone = timeZone;
return this;
}
[JsonProperty("pre_zone_adjust_large_interval")]
internal bool? _PreZoneAdjustLargeInterval { get; set; }

public DateHistogramAggregationDescriptor<T> PreZoneAdjustLargeInterval(bool adjustLargeInterval = true)
{
this._PreZoneAdjustLargeInterval = adjustLargeInterval;
return this;
}
[JsonProperty("factor")]
internal int? _Factor { get; set; }

public DateHistogramAggregationDescriptor<T> Interval(int factor)
{
this._Factor = factor;
return this;
}

[JsonProperty("pre_offset")]
internal string _PreOffset { get; set; }

public DateHistogramAggregationDescriptor<T> PreOffset(string preOffset)
{
this._PreOffset = preOffset;
return this;
}
[JsonProperty("post_offset")]
internal string _PostOffset { get; set; }

public DateHistogramAggregationDescriptor<T> PostOffset(string postOffset)
{
this._PostOffset = postOffset;
return this;
}

[JsonProperty("order")]
internal IDictionary<string, string> _Order { get; set; }

public DateHistogramAggregationDescriptor<T> OrderAscending(string key)
{
this._Order = new Dictionary<string, string> { {key, "asc"}};
return this;
}

public DateHistogramAggregationDescriptor<T> OrderDescending(string key)
{
this._Order = new Dictionary<string, string> { {key, "asc"}};
return this;
}

}
}
Loading