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
11 changes: 11 additions & 0 deletions src/Nest/Aggregations/AggregationContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ public interface IAggregationContainer
[JsonProperty("matrix_stats")]
IMatrixStatsAggregation MatrixStats { get; set; }

[JsonProperty("adjacency_matrix")]
IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }

[JsonProperty("aggs")]
AggregationDictionary Aggregations { get; set; }

Expand Down Expand Up @@ -290,6 +293,8 @@ public class AggregationContainer : IAggregationContainer

public IMatrixStatsAggregation MatrixStats { get; set; }

public IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }

public AggregationDictionary Aggregations { get; set; }

public static implicit operator AggregationContainer(AggregationBase aggregator)
Expand Down Expand Up @@ -407,6 +412,8 @@ public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationConta

IMatrixStatsAggregation IAggregationContainer.MatrixStats { get; set; }

IAdjacencyMatrixAggregation IAggregationContainer.AdjacencyMatrix { get; set; }

public AggregationContainerDescriptor<T> Average(string name,
Func<AverageAggregationDescriptor<T>, IAverageAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.Average = d);
Expand Down Expand Up @@ -591,6 +598,10 @@ public AggregationContainerDescriptor<T> MatrixStats(string name,
Func<MatrixStatsAggregationDescriptor<T>, IMatrixStatsAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.MatrixStats = d);

public AggregationContainerDescriptor<T> AdjacencyMatrix(string name,
Func<AdjacencyMatrixAggregationDescriptor<T>, IAdjacencyMatrixAggregation> selector) =>
_SetInnerAggregation(name, selector, (a, d) => a.AdjacencyMatrix = d);

/// <summary>
/// Fluent methods do not assign to properties on `this` directly but on IAggregationContainers inside `this.Aggregations[string, IContainer]
/// </summary>
Expand Down
78 changes: 40 additions & 38 deletions src/Nest/Aggregations/AggregationsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Globalization;
using System.Linq;

namespace Nest
Expand All @@ -11,11 +11,11 @@ public class AggregationsHelper

public AggregationsHelper() { }

protected AggregationsHelper(IDictionary<string, IAggregate> aggregations)
{
this.Aggregations = aggregations != null ?
new Dictionary<string, IAggregate>(aggregations)
: EmptyReadOnly<string, IAggregate>.Dictionary;
protected AggregationsHelper(IDictionary<string, IAggregate> aggregations)
{
this.Aggregations = aggregations != null ?
new Dictionary<string, IAggregate>(aggregations)
: EmptyReadOnly<string, IAggregate>.Dictionary;
}
public AggregationsHelper(IReadOnlyDictionary<string, IAggregate> aggregations)
{
Expand Down Expand Up @@ -59,20 +59,20 @@ public ScriptedMetricAggregate ScriptedMetric(string key)
: this.TryGet<ScriptedMetricAggregate>(key);
}

public StatsAggregate Stats(string key) => this.TryGet<StatsAggregate>(key);
public StatsAggregate StatsBucket(string key) => this.TryGet<StatsAggregate>(key);
public ExtendedStatsAggregate ExtendedStats(string key) => this.TryGet<ExtendedStatsAggregate>(key);
public ExtendedStatsAggregate ExtendedStatsBucket(string key) => this.TryGet<ExtendedStatsAggregate>(key);
public StatsAggregate Stats(string key) => this.TryGet<StatsAggregate>(key);

public StatsAggregate StatsBucket(string key) => this.TryGet<StatsAggregate>(key);

public ExtendedStatsAggregate ExtendedStats(string key) => this.TryGet<ExtendedStatsAggregate>(key);

public ExtendedStatsAggregate ExtendedStatsBucket(string key) => this.TryGet<ExtendedStatsAggregate>(key);

public GeoBoundsAggregate GeoBounds(string key) => this.TryGet<GeoBoundsAggregate>(key);

public PercentilesAggregate Percentiles(string key) => this.TryGet<PercentilesAggregate>(key);
public PercentilesAggregate PercentilesBucket(string key) => this.TryGet<PercentilesAggregate>(key);
public PercentilesAggregate Percentiles(string key) => this.TryGet<PercentilesAggregate>(key);

public PercentilesAggregate PercentilesBucket(string key) => this.TryGet<PercentilesAggregate>(key);

public PercentilesAggregate PercentileRanks(string key) => this.TryGet<PercentilesAggregate>(key);

public TopHitsAggregate TopHits(string key) => this.TryGet<TopHitsAggregate>(key);
Expand All @@ -84,8 +84,8 @@ public FiltersAggregate Filters(string key)
return named;

var anonymous = this.TryGet<BucketAggregate>(key);
return anonymous != null
? new FiltersAggregate { Buckets = anonymous.Items.OfType<FiltersBucketItem>().ToList(), Meta = anonymous.Meta }
return anonymous != null
? new FiltersAggregate { Buckets = anonymous.Items.OfType<FiltersBucketItem>().ToList(), Meta = anonymous.Meta }
: null;
}

Expand All @@ -101,8 +101,8 @@ public FiltersAggregate Filters(string key)

public SingleBucketAggregate Children(string key) => this.TryGet<SingleBucketAggregate>(key);

public SingleBucketAggregate Sampler(string key) => this.TryGet<SingleBucketAggregate>(key);
public SingleBucketAggregate Sampler(string key) => this.TryGet<SingleBucketAggregate>(key);

public GeoCentroidAggregate GeoCentroid(string key) => this.TryGet<GeoCentroidAggregate>(key);

public SignificantTermsAggregate SignificantTerms(string key)
Expand Down Expand Up @@ -138,6 +138,8 @@ public TermsAggregate<TKey> Terms<TKey>(string key)

public MultiBucketAggregate<KeyedBucket<string>> GeoHash(string key) => GetMultiKeyedBucketAggregate<string>(key);

public MultiBucketAggregate<KeyedBucket<string>> AdjacencyMatrix(string key) => GetMultiKeyedBucketAggregate<string>(key);

public MultiBucketAggregate<RangeBucket> Range(string key) => GetMultiBucketAggregate<RangeBucket>(key);

public MultiBucketAggregate<RangeBucket> DateRange(string key) => GetMultiBucketAggregate<RangeBucket>(key);
Expand All @@ -148,7 +150,7 @@ public TermsAggregate<TKey> Terms<TKey>(string key)

public MultiBucketAggregate<DateHistogramBucket> DateHistogram(string key) => GetMultiBucketAggregate<DateHistogramBucket>(key);

public MatrixStatsAggregate MatrixStats(string key) => this.TryGet<MatrixStatsAggregate>(key);
public MatrixStatsAggregate MatrixStats(string key) => this.TryGet<MatrixStatsAggregate>(key);


private TAggregate TryGet<TAggregate>(string key)
Expand All @@ -168,19 +170,19 @@ private MultiBucketAggregate<TBucket> GetMultiBucketAggregate<TBucket>(string ke
Buckets = bucket.Items.OfType<TBucket>().ToList(),
Meta = bucket.Meta,
};
}
private MultiBucketAggregate<KeyedBucket<TKey>> GetMultiKeyedBucketAggregate<TKey>(string key)
{
var bucket = this.TryGet<BucketAggregate>(key);
if (bucket == null) return null;
return new MultiBucketAggregate<KeyedBucket<TKey>>
{
Buckets = GetKeyedBuckets<TKey>(bucket.Items).ToList(),
Meta = bucket.Meta,
};
}
}
private MultiBucketAggregate<KeyedBucket<TKey>> GetMultiKeyedBucketAggregate<TKey>(string key)
{
var bucket = this.TryGet<BucketAggregate>(key);
if (bucket == null) return null;
return new MultiBucketAggregate<KeyedBucket<TKey>>
{
Buckets = GetKeyedBuckets<TKey>(bucket.Items).ToList(),
Meta = bucket.Meta,
};
}


private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket> items)
{
var buckets = items.Cast<KeyedBucket<object>>();
Expand All @@ -197,4 +199,4 @@ private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Newtonsoft.Json;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[ContractJsonConverter(typeof(AggregationJsonConverter<AdjacencyMatrixAggregation>))]
public interface IAdjacencyMatrixAggregation : IBucketAggregation
{
[JsonProperty("filters")]
INamedFiltersContainer Filters { get; set; }
}

public class AdjacencyMatrixAggregation : BucketAggregationBase, IAdjacencyMatrixAggregation
{
internal AdjacencyMatrixAggregation() { }

public AdjacencyMatrixAggregation(string name) : base(name) { }

public INamedFiltersContainer Filters { get; set; }

internal override void WrapInContainer(AggregationContainer c) => c.AdjacencyMatrix = this;
}

public class AdjacencyMatrixAggregationDescriptor<T>
: BucketAggregationDescriptorBase<AdjacencyMatrixAggregationDescriptor<T>, IAdjacencyMatrixAggregation, T>
, IAdjacencyMatrixAggregation
where T : class
{
INamedFiltersContainer IAdjacencyMatrixAggregation.Filters { get; set; }

public AdjacencyMatrixAggregationDescriptor<T> Filters(Func<NamedFiltersContainerDescriptor<T>, IPromise<INamedFiltersContainer>> selector) =>
Assign(a => a.Filters = selector?.Invoke(new NamedFiltersContainerDescriptor<T>())?.Value);

}
}
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Aggregations\AggregationContainer.cs" />
<Compile Include="Aggregations\AggregationJsonConverter.cs" />
<Compile Include="Aggregations\AggregationsHelper.cs" />
<Compile Include="Aggregations\Bucket\AdjacencyMatrix\AdjacencyMatrixAggregation.cs" />
<Compile Include="Aggregations\Bucket\Bucket.cs" />
<Compile Include="Aggregations\Bucket\BucketAggregate.cs" />
<Compile Include="Aggregations\Bucket\BucketAggregation.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
using Tests.Framework.MockData;
using Tests.Search.Request;

namespace Tests.Aggregations.Bucket.AdjacencyMatrix
{
public class AdjacencyMatrixUsageTests : AggregationUsageTestBase
{
public AdjacencyMatrixUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage)
{
}

protected override object ExpectJson => new
{
size = 0,
aggs = new
{
interactions = new
{
adjacency_matrix = new
{
filters = new
{
grpA = new { term = new { state = new { value = "BellyUp"} } },
grpB = new { term = new { state = new { value = "Stable"} } },
grpC = new { term = new { state = new { value = "VeryActive"} } }
}
}
}
}
};

protected override SearchRequest<Project> Initializer =>
new SearchRequest<Project>
{
Size = 0,
Aggregations = new AdjacencyMatrixAggregation("interactions")
{
Filters = new NamedFiltersContainer
{
{ "grpA", new TermQuery { Field = "state", Value = StateOfBeing.BellyUp } },
{ "grpB", new TermQuery { Field = "state", Value = StateOfBeing.Stable } },
{ "grpC", new TermQuery { Field = "state", Value = StateOfBeing.VeryActive } },
}
}
};

protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
.Size(0)
.Aggregations(aggs => aggs
.AdjacencyMatrix("interactions", am => am
.Filters(fs => fs
.Filter("grpA", f => f.Term(p => p.State, StateOfBeing.BellyUp))
.Filter("grpB", f => f.Term(p => p.State, StateOfBeing.Stable))
.Filter("grpC", f => f.Term(p => p.State, StateOfBeing.VeryActive))
)
)
);

protected override void ExpectResponse(ISearchResponse<Project> response)
{
response.ShouldBeValid();
var interactions = response.Aggs.AdjacencyMatrix("interactions");
interactions.Should().NotBeNull();
var buckets = interactions.Buckets;
buckets.Should().NotBeNullOrEmpty();
foreach (var bucket in buckets)
{
bucket.Key.Should().NotBeNullOrEmpty();
bucket.DocCount.Should().BeGreaterThan(0);
}
}
}
}
1 change: 1 addition & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<Compile Include=".\Document\Single\Update\UpdateApiTests.cs" />
<Compile Include=".\Document\Single\Update\UpdateUrlTests.cs" />
<Compile Include=".\Document\Single\Update\UpdateWithSourceApiTests.cs" />
<Compile Include="Aggregations\Bucket\AdjacencyMatrix\AdjacencyMatrixUsageTests.cs" />
<Compile Include="Aggregations\Matrix\MatrixStats\MatrixStatsAggregationUsageTests.cs" />
<Compile Include="Aggregations\Metric\GeoCentroid\GeoCentroidAggregationUsageTests.cs" />
<Compile Include="Aggregations\ReservedAggNames.doc.cs" />
Expand Down