Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class MultiBucketAggregate<TBucket> : BucketAggregateBase
where TBucket : IBucket
{
public MultiBucketAggregate() { }

[System.Text.Json.Serialization.JsonConstructor]
public MultiBucketAggregate(IReadOnlyDictionary<string, IAggregate> aggregations) : base(aggregations) { }

public IReadOnlyCollection<TBucket> Buckets { get; set; } = EmptyReadOnly<TBucket>.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;

namespace Foundatio.Repositories.Models;
Expand All @@ -7,6 +7,8 @@ namespace Foundatio.Repositories.Models;
public class SingleBucketAggregate : BucketAggregateBase
{
public SingleBucketAggregate() { }

[System.Text.Json.Serialization.JsonConstructor]
public SingleBucketAggregate(IReadOnlyDictionary<string, IAggregate> aggregations) : base(aggregations) { }

public long Total { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Foundatio.Repositories.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -36,7 +37,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
value = new PercentilesAggregate();
break;
case "sbucket":
value = new SingleBucketAggregate();
var aggregationsToken = item.SelectToken("Aggregations") ?? item.SelectToken("aggregations");
var aggregations = aggregationsToken?.ToObject<IReadOnlyDictionary<string, IAggregate>>(serializer);
value = new SingleBucketAggregate(aggregations);
break;
case "stats":
value = new StatsAggregate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Threading.Tasks;
using Exceptionless.DateTimeExtensions;
using Foundatio.Repositories.Elasticsearch.Extensions;
using Foundatio.Repositories.Elasticsearch.Tests.Repositories.Models;
using Foundatio.Repositories.Models;
using Microsoft.Extensions.Time.Testing;
Expand Down Expand Up @@ -106,47 +105,6 @@ await _employeeRepository.AddAsync(new Employee
Assert.Equal(1, result.Aggregations.Cardinality("cardinality_twitter").Value);
}

[Fact]
public async Task GetNestedAggregationsAsync()
{
var utcToday = new DateTimeOffset(DateTime.UtcNow.Year, 1, 1, 12, 0, 0, TimeSpan.FromHours(5));
var employees = new List<Employee> {
EmployeeGenerator.Generate(nextReview: utcToday.SubtractDays(2)),
EmployeeGenerator.Generate(nextReview: utcToday.SubtractDays(1))
};
employees[0].Id = "employee1";
employees[0].Id = "employee2";
employees[0].PeerReviews = new PeerReview[] { new PeerReview { ReviewerEmployeeId = employees[1].Id, Rating = 4 } };
employees[1].PeerReviews = new PeerReview[] { new PeerReview { ReviewerEmployeeId = employees[0].Id, Rating = 5 } };

await _employeeRepository.AddAsync(employees, o => o.ImmediateConsistency());

var nestedAggQuery = _client.Search<Employee>(d => d.Index("employees").Aggregations(a => a
.Nested("nested_reviewRating", h => h.Path("peerReviews")
.Aggregations(a1 => a1.Terms("terms_rating", t => t.Field("peerReviews.rating").Meta(m => m.Add("@field_type", "integer")))))
));

var result = nestedAggQuery.Aggregations.ToAggregations();
Assert.Single(result);
Assert.Equal(2, ((result["nested_reviewRating"] as Foundatio.Repositories.Models.SingleBucketAggregate).Aggregations["terms_rating"] as Foundatio.Repositories.Models.BucketAggregate).Items.Count);

var nestedAggQueryWithFilter = _client.Search<Employee>(d => d.Index("employees").Aggregations(a => a
.Nested("nested_reviewRating", h => h.Path("peerReviews")
.Aggregations(a1 => a1
.Filter("user_" + employees[0].Id, f => f.Filter(q => q.Term(t => t.Field("peerReviews.reviewerEmployeeId").Value(employees[0].Id)))
.Aggregations(a2 => a2.Terms("terms_rating", t => t.Field("peerReviews.rating").Meta(m => m.Add("@field_type", "integer")))))
))));

result = nestedAggQueryWithFilter.Aggregations.ToAggregations();
Assert.Single(result);

var filteredAgg = ((result["nested_reviewRating"] as Foundatio.Repositories.Models.SingleBucketAggregate).Aggregations["user_" + employees[0].Id] as Foundatio.Repositories.Models.SingleBucketAggregate);
Assert.NotNull(filteredAgg);
Assert.Single(filteredAgg.Aggregations.Terms("terms_rating").Buckets);
Assert.Equal("5", filteredAgg.Aggregations.Terms("terms_rating").Buckets.First().Key);
Assert.Equal(1, filteredAgg.Aggregations.Terms("terms_rating").Buckets.First().Total);
}

[Fact]
public async Task GetAliasedNumberAggregationThatCausesMappingAsync()
{
Expand Down
Loading
Loading