Skip to content

Reverse nested aggregation support #939

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 1 commit into from
Sep 11, 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
17 changes: 17 additions & 0 deletions src/Nest/DSL/Aggregations/AggregationDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public interface IAggregationContainer
[JsonProperty("nested")]
INestedAggregator Nested { get; set; }

[JsonProperty("reverse_nested")]
IReverseNestedAggregator ReverseNested { get; set; }

[JsonProperty("range")]
IRangeAggregator Range { get; set; }

Expand Down Expand Up @@ -102,6 +105,7 @@ public class AggregationContainer : IAggregationContainer
private ICardinalityAggregator _cardinality;
private IMissingAggregator _missing;
private INestedAggregator _nested;
private IReverseNestedAggregator _reverseNested;
private IRangeAggregator _range;
private ITermsAggregator _terms;
private ISignificantTermsAggregator _significantTerms;
Expand Down Expand Up @@ -193,6 +197,12 @@ public INestedAggregator Nested
set { _nested = value; }
}

public IReverseNestedAggregator ReverseNested
{
get { return _reverseNested; }
set { _reverseNested = value; }
}

public IRangeAggregator Range
{
get { return _range; }
Expand Down Expand Up @@ -264,6 +274,8 @@ public class AggregationDescriptor<T> : IAggregationContainer
IMissingAggregator IAggregationContainer.Missing { get; set; }

INestedAggregator IAggregationContainer.Nested { get; set; }

IReverseNestedAggregator IAggregationContainer.ReverseNested { get; set; }

IRangeAggregator IAggregationContainer.Range { get; set; }

Expand Down Expand Up @@ -381,6 +393,11 @@ public AggregationDescriptor<T> Nested(string name, Func<NestedAggregationDescri
return _SetInnerAggregation(name, selector, (a, d) => a.Nested = d);
}

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

public AggregationDescriptor<T> Range(string name, Func<RangeAggregationDescriptor<T>, RangeAggregationDescriptor<T>> selector)
{
return _SetInnerAggregation(name, selector, (a, d) => a.Range = d);
Expand Down
44 changes: 44 additions & 0 deletions src/Nest/DSL/Aggregations/ReverseNestedAggregationDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeConverter<ReverseNestedAggregator>))]
public interface IReverseNestedAggregator : IBucketAggregator
{
[JsonProperty("path")]
PropertyPathMarker Path { get; set; }
}

public class ReverseNestedAggregator : BucketAggregator, IReverseNestedAggregator
{
[JsonProperty("path")]
public PropertyPathMarker Path { get; set; }
}

public class ReverseNestedAggregationDescriptor<T>
: BucketAggregationBaseDescriptor<ReverseNestedAggregationDescriptor<T>, T>, IReverseNestedAggregator
where T : class
{
IReverseNestedAggregator Self { get { return this; } }
PropertyPathMarker IReverseNestedAggregator.Path { get; set; }

public ReverseNestedAggregationDescriptor<T> Path(string path)
{
this.Self.Path = path;
return this;
}

public ReverseNestedAggregationDescriptor<T> Path(Expression<Func<T, object>> path)
{
this.Self.Path = path;
return this;
}
}
}
5 changes: 5 additions & 0 deletions src/Nest/Domain/Aggregations/AggregationsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public SingleBucket Nested(string key)
return this.TryGet<SingleBucket>(key);
}

public SingleBucket ReverseNested(string key)
{
return this.TryGet<SingleBucket>(key);
}

public BucketWithDocCount<SignificantTermItem> SignificantTerms(string key)
{
var bucket = this.TryGet<BucketWithDocCount>(key);
Expand Down
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
<Compile Include="Domain\Stats\SegmentsStats.cs" />
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
<Compile Include="DSL\CatIndicesDescriptor.cs" />
<Compile Include="DSL\CatShardsDescriptor.cs" />
<Compile Include="DSL\CatThreadPoolDescriptor.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,44 @@ public void Terms()
.And.BeGreaterOrEqualTo(18);
}

[Test]
public void ReverseNested()
{
var results = this.Client.Search<ElasticsearchProject>(s => s
.Size(0)
.Aggregations(a => a
.Nested("contributors", n => n
.Path(p => p.Contributors)
.Aggregations(t => t
.Terms("ages", m => m
.Field(p => p.Contributors.First().Age)
.Aggregations(aa => aa
.ReverseNested("contributor_to_project", rn => rn
.Aggregations(aaa => aaa
.Terms("countries_per_age", tt => tt
.Field(p => p.Country)
)
)
)
)
)
)
)
)
);

results.IsValid.Should().BeTrue();
var contributors = results.Aggs.Nested("contributors");
var ages = contributors.Aggregations["ages"] as Bucket;

foreach (var item in ages.Items)
{
var age = item as KeyItem;
age.Key.Should().NotBeNullOrWhiteSpace();
var contributorToProject = age.Aggregations["contributor_to_project"] as SingleBucket;
var countriesPerAge = contributorToProject.Terms("countries_per_age");
countriesPerAge.Items.Count().Should().BeGreaterThan(0);
}
}
}
}