Skip to content

Scripted metric aggregation support #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 12, 2014
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
20 changes: 19 additions & 1 deletion src/Nest/DSL/Aggregations/AggregationDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public interface IAggregationContainer
[JsonProperty("top_hits")]
ITopHitsAggregator TopHits { get; set; }

[JsonProperty("scripted_metric")]
IScriptedMetricAggregator ScriptedMetric { get; set; }

[JsonProperty("aggs")]
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
IDictionary<string, IAggregationContainer> Aggregations { get; set; }
Expand Down Expand Up @@ -117,8 +120,9 @@ public class AggregationContainer : IAggregationContainer
private ISignificantTermsAggregator _significantTerms;
private IPercentileRanksAggregaor _percentileRanks;
private IFiltersAggregator _filters;

private ITopHitsAggregator _topHits;
private IScriptedMetricAggregator _scriptedMetric;

public IAverageAggregator Average { get; set; }
public IValueCountAggregator ValueCount { get; set; }
public IMaxAggregator Max { get; set; }
Expand Down Expand Up @@ -246,6 +250,12 @@ public ITopHitsAggregator TopHits
set { _topHits = value; }
}

public IScriptedMetricAggregator ScriptedMetric
{
get { return _scriptedMetric; }
set { _scriptedMetric = value; }
}

private void LiftAggregations(IBucketAggregator bucket)
{
if (bucket == null) return;
Expand Down Expand Up @@ -314,6 +324,8 @@ public class AggregationDescriptor<T> : IAggregationContainer

ITopHitsAggregator IAggregationContainer.TopHits { get; set; }

IScriptedMetricAggregator IAggregationContainer.ScriptedMetric { get; set; }

public AggregationDescriptor<T> Average(string name, Func<AverageAggregationDescriptor<T>, AverageAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a.Average = d);
Expand Down Expand Up @@ -464,6 +476,12 @@ public AggregationDescriptor<T> TopHits(string name,
return _SetInnerAggregation(name, selector, (a, d) => a.TopHits = d);
}

public AggregationDescriptor<T> ScriptedMetric(string name,
Func<ScriptedMetricAggregationDescriptor<T>, ScriptedMetricAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a.ScriptedMetric = d);
}

private AggregationDescriptor<T> _SetInnerAggregation<TAggregation>(
string key,
Func<TAggregation, TAggregation> selector
Expand Down
169 changes: 169 additions & 0 deletions src/Nest/DSL/Aggregations/ScriptedMetricAggregationDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeConverter<ScriptedMetricsAggregator>))]
public interface IScriptedMetricAggregator
{
[JsonProperty("init_script")]
string InitScript { get; set; }

[JsonProperty("init_script_file")]
string InitScriptFile { get; set; }

[JsonProperty("init_script_id")]
string InitScriptId { get; set; }

[JsonProperty("map_script")]
string MapScript { get; set; }

[JsonProperty("map_script_file")]
string MapScriptFile { get; set; }

[JsonProperty("map_script_id")]
string MapScriptId { get; set; }

[JsonProperty("combine_script")]
string CombineScript { get; set; }

[JsonProperty("combine_script_file")]
string CombineScriptFile { get; set; }

[JsonProperty("combine_script_id")]
string CombineScriptId { get; set; }

[JsonProperty("reduce_script")]
string ReduceScript { get; set; }

[JsonProperty("reduce_script_file")]
string ReduceScriptFile { get; set; }

[JsonProperty("reduce_script_id")]
string ReduceScriptId { get; set; }

[JsonProperty("reduce_params")]
IDictionary<string, object> ReduceParams { get; set; }
}

public class ScriptedMetricsAggregator : MetricAggregator, IScriptedMetricAggregator
{
public string InitScript { get; set; }
public string InitScriptFile { get; set; }
public string InitScriptId { get; set; }
public string MapScript { get; set; }
public string MapScriptFile { get; set; }
public string MapScriptId { get; set; }
public string CombineScript { get; set; }
public string CombineScriptFile { get; set; }
public string CombineScriptId { get; set; }
public string ReduceScript { get; set; }
public string ReduceScriptFile { get; set; }
public string ReduceScriptId { get; set; }
public IDictionary<string, object> ReduceParams { get; set; }
}

public class ScriptedMetricAggregationDescriptor<T>
: MetricAggregationBaseDescriptor<ScriptedMetricAggregationDescriptor<T>, T>, IScriptedMetricAggregator
where T : class
{
IScriptedMetricAggregator Self { get { return this; } }

string IScriptedMetricAggregator.InitScript { get; set; }
string IScriptedMetricAggregator.InitScriptFile { get; set; }
string IScriptedMetricAggregator.InitScriptId { get; set; }
string IScriptedMetricAggregator.MapScript { get; set; }
string IScriptedMetricAggregator.MapScriptFile { get; set; }
string IScriptedMetricAggregator.MapScriptId { get; set; }
string IScriptedMetricAggregator.CombineScript { get; set; }
string IScriptedMetricAggregator.CombineScriptFile { get; set; }
string IScriptedMetricAggregator.CombineScriptId { get; set; }
string IScriptedMetricAggregator.ReduceScript { get; set; }
string IScriptedMetricAggregator.ReduceScriptFile { get; set; }
string IScriptedMetricAggregator.ReduceScriptId { get; set; }
IDictionary<string, object> IScriptedMetricAggregator.ReduceParams { get; set; }

public ScriptedMetricAggregationDescriptor<T> InitScript(string script)
{
this.Self.InitScript = script;
return this;
}

public ScriptedMetricAggregationDescriptor<T> InitScriptFile(string file)
{
this.Self.InitScriptFile = file;
return this;
}

public ScriptedMetricAggregationDescriptor<T> InitScriptId(string id)
{
this.Self.InitScriptId = id;
return this;
}

public ScriptedMetricAggregationDescriptor<T> MapScript(string script)
{
this.Self.MapScript = script;
return this;
}

public ScriptedMetricAggregationDescriptor<T> MapScriptFile(string file)
{
this.Self.MapScriptFile = file;
return this;
}

public ScriptedMetricAggregationDescriptor<T> MapScriptId(string id)
{
this.Self.MapScriptId = id;
return this;
}

public ScriptedMetricAggregationDescriptor<T> CombineScript(string script)
{
this.Self.CombineScript = script;
return this;
}

public ScriptedMetricAggregationDescriptor<T> CombineScriptFile(string file)
{
this.Self.CombineScriptFile = file;
return this;
}

public ScriptedMetricAggregationDescriptor<T> CombineScriptId(string id)
{
this.Self.CombineScriptId = id;
return this;
}

public ScriptedMetricAggregationDescriptor<T> ReduceScript(string script)
{
this.Self.ReduceScript = script;
return this;
}

public ScriptedMetricAggregationDescriptor<T> ReduceScriptFile(string file)
{
this.Self.ReduceScriptFile = file;
return this;
}

public ScriptedMetricAggregationDescriptor<T> ReduceScriptId(string id)
{
this.Self.ReduceScriptId = id;
return this;
}

public ScriptedMetricAggregationDescriptor<T> ReduceParams(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramSelector)
{
this.Self.ReduceParams = paramSelector(new FluentDictionary<string, object>());
return this;
}
}
}
12 changes: 12 additions & 0 deletions src/Nest/Domain/Aggregations/AggregationsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public ValueMetric ValueCount(string key)
return this.TryGet<ValueMetric>(key);
}

public ScriptedValueMetric ScriptedMetric(string key)
{
var valueMetric = this.TryGet<ValueMetric>(key);

if (valueMetric != null)
{
return new ScriptedValueMetric { _Value = valueMetric.Value };
}

return this.TryGet<ScriptedValueMetric>(key);
}

public StatsMetric Stats(string key)
{
return this.TryGet<StatsMetric>(key);
Expand Down
28 changes: 28 additions & 0 deletions src/Nest/Domain/Aggregations/ScriptedValueMetric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public class ScriptedValueMetric : IMetricAggregation
{
internal object _Value { get; set; }

/// <summary>
/// Get the resut of the scripted metric aggregation as T
/// </summary>
/// <typeparam name="T">The type that best represents the result of your scripted metric aggrgation</typeparam>
public T Value<T>()
{
var jToken = this._Value as JToken;

if (jToken != null)
return jToken.ToObject<T>();

return (T)this._Value;
}
}
}
2 changes: 2 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Domain\Aggregations\Bucket.cs" />
<Compile Include="Domain\Aggregations\BucketAggregationBase.cs" />
<Compile Include="Domain\Aggregations\FiltersBucket.cs" />
<Compile Include="Domain\Aggregations\ScriptedValueMetric.cs" />
<Compile Include="Domain\Aggregations\TopHitsMetric.cs" />
<Compile Include="Domain\Aggregations\GeoBoundsMetric.cs" />
<Compile Include="Domain\Aggregations\HistogramItem.cs" />
Expand Down Expand Up @@ -233,6 +234,7 @@
<Compile Include="Domain\Suggest\SuggestField.cs" />
<Compile Include="DSL\Aggregations\FiltersAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\ScriptedMetricAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
Expand Down
21 changes: 16 additions & 5 deletions src/Nest/Resolvers/Converters/Aggregations/AggregationConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,25 @@ private IAggregation GetBucketAggregation(JsonReader reader, JsonSerializer seri
private IAggregation GetValueMetricOrAggregation(JsonReader reader, JsonSerializer serializer)
{
reader.Read();
var metric = new ValueMetric()
var valueMetric = new ValueMetric()
{
Value = (reader.Value as double?)
};
if (metric.Value == null && reader.ValueType == typeof(long))
metric.Value = reader.Value as long?;
reader.Read();
return metric;
if (valueMetric.Value == null && reader.ValueType == typeof(long))
valueMetric.Value = reader.Value as long?;

if (valueMetric.Value != null)
{
reader.Read();
return valueMetric;
}

var scriptedMetric = serializer.Deserialize(reader);

if (scriptedMetric != null)
return new ScriptedValueMetric { _Value = scriptedMetric };

return valueMetric;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Expand Down
Loading