|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using FluentAssertions; |
| 8 | + |
| 9 | +namespace Nest.Tests.Integration.Reproduce |
| 10 | +{ |
| 11 | + public class ElasticCallCustomer |
| 12 | + { |
| 13 | + public int Id { get; set; } |
| 14 | + |
| 15 | + public string Name { get; set; } |
| 16 | + |
| 17 | + } |
| 18 | + public class ElasticCall |
| 19 | + { |
| 20 | + public int Id { get; set; } |
| 21 | + |
| 22 | + public long Duration { get; set; } |
| 23 | + |
| 24 | + public ElasticCallCustomer Customer { get; set; } |
| 25 | + } |
| 26 | + |
| 27 | + [TestFixture] |
| 28 | + public class Reproduce986Tests : IntegrationTests |
| 29 | + { |
| 30 | + [Test] |
| 31 | + public void PrefixQueryShouldReturnHits() |
| 32 | + { |
| 33 | + var customer1 = new ElasticCallCustomer { Id = 1, Name = "Martijn" }; |
| 34 | + var customer2 = new ElasticCallCustomer { Id = 2, Name = "Thomas Tvedt" }; |
| 35 | + |
| 36 | + var calls = new[] |
| 37 | + { |
| 38 | + new ElasticCall {Id = 1, Duration=12315, Customer = customer1 }, |
| 39 | + new ElasticCall {Id = 2, Duration=345345, Customer = customer2 }, |
| 40 | + new ElasticCall {Id = 3, Duration=231, Customer = customer1 }, |
| 41 | + new ElasticCall {Id = 4, Duration=908, Customer = customer1 }, |
| 42 | + new ElasticCall {Id = 5, Duration=1231, Customer = customer2 }, |
| 43 | + new ElasticCall {Id = 6, Duration=2112, Customer = customer1 }, |
| 44 | + new ElasticCall {Id = 7, Duration=99891, Customer = customer1 }, |
| 45 | + new ElasticCall {Id = 8, Duration=29401281, Customer = customer1 }, |
| 46 | + new ElasticCall {Id = 9, Duration=2, Customer = customer1 }, |
| 47 | + new ElasticCall {Id = 10, Duration=21231, Customer = customer1 }, |
| 48 | + }; |
| 49 | + |
| 50 | + var result = this.Client.Bulk(b => b.IndexMany(calls).Refresh()); |
| 51 | + result.IsValid.Should().BeTrue(); |
| 52 | + |
| 53 | + var search = this.Client.Search<ElasticCall>(s => s |
| 54 | + .Aggregations(agg => agg |
| 55 | + .Terms("calls_per_customer", calls_per_customer => calls_per_customer |
| 56 | + .Field(call=>call.Customer.Id) |
| 57 | + .Size(10) |
| 58 | + .Aggregations(callsPerCustomerAggs => callsPerCustomerAggs |
| 59 | + .Terms("customer_name", customer_name => customer_name |
| 60 | + .Script("_source.customer.name") |
| 61 | + .Size(1) |
| 62 | + ) |
| 63 | + .Sum("total_duration", total_duration => total_duration.Field("duration")) |
| 64 | + .Average("average_duration", average_duration => average_duration.Field("duration")) |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + ); |
| 69 | + |
| 70 | + search.IsValid.Should().BeTrue(); |
| 71 | + |
| 72 | + var callsPerCustomer = search.Aggs.Terms("calls_per_customer"); |
| 73 | + callsPerCustomer.Items.Should().NotBeEmpty().And.HaveCount(2); |
| 74 | + |
| 75 | + foreach(var term in callsPerCustomer.Items) |
| 76 | + { |
| 77 | + var name = term.Terms("customer_name"); |
| 78 | + name.Items.Should().NotBeEmpty().And.HaveCount(1); |
| 79 | + |
| 80 | + var sum = term.Sum("total_duration").Value; |
| 81 | + sum.Should().BeGreaterThan(10); |
| 82 | + |
| 83 | + var average = term.Average("average_duration").Value; |
| 84 | + average.Should().BeGreaterThan(10); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments