Вот полный пример работы с NEST 6.1.0
private static void Main()
{
var index = "default";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex(index);
var client = new ElasticClient(connectionSettings);
if (client.IndexExists(index).Exists)
client.DeleteIndex(index);
client.CreateIndex(index, c => c
.Mappings(m => m
.Map<MyDocument>(mm => mm
.AutoMap()
.Properties(p => p
.Nested<Rate>(n => n
.AutoMap()
.Name(nn => nn.Rates)
)
)
)
)
);
client.Bulk(b => b
.IndexMany(new[] {
new MyDocument
{
Name = "doc 1",
Rates = new []
{
new Rate
{
Start = new DateTime(2018, 6, 9),
End = new DateTime(2018, 6, 16),
WeeklyRate = 100
},
new Rate
{
Start = new DateTime(2018, 6, 16),
End = new DateTime(2018, 6, 23),
WeeklyRate = 200
}
}
},
new MyDocument
{
Name = "doc 2",
Rates = new []
{
new Rate
{
Start = new DateTime(2018, 6, 9),
End = new DateTime(2018, 6, 16),
WeeklyRate = 120
},
new Rate
{
Start = new DateTime(2018, 6, 16),
End = new DateTime(2018, 6, 23),
WeeklyRate = 250
}
}
}
})
.Refresh(Refresh.WaitFor)
);
var searchResponse = client.Search<MyDocument>(s => s
// apply your filtering in .Query(...) e.g. applicable date range
.Query(q => q.MatchAll())
// don't return documents, just calculate aggregations
.Size(0)
.Aggregations(a => a
.Nested("nested_start_dates", n => n
.Path(f => f.Rates)
.Aggregations(aa => aa
.DateHistogram("start_dates", dh => dh
.Field(f => f.Rates.First().Start)
.Interval(DateInterval.Day)
.MinimumDocumentCount(1)
.Aggregations(aaa => aaa
.Min("min_rate", m => m
.Field(f => f.Rates.First().WeeklyRate)
)
.Max("max_rate", m => m
.Field(f => f.Rates.First().WeeklyRate)
)
)
)
)
)
)
);
var nested = searchResponse.Aggregations.Nested("nested_start_dates");
var startBuckets = nested.DateHistogram("start_dates").Buckets;
foreach(var start in startBuckets)
{
var min = start.Min("min_rate").Value;
var max = start.Max("max_rate").Value;
Console.WriteLine($"{start.KeyAsString} - min: {min}, max: {max}");
}
}
public class MyDocument
{
public string Name {get;set;}
public IEnumerable<Rate> Rates {get;set;}
}
public class Rate
{
public DateTime Start {get;set;}
public DateTime End {get;set;}
public double WeeklyRate {get;set;}
}
, который выводит на консоль следующую информацию
2018-06-09T00:00:00.000Z - min: 100, max: 120
2018-06-16T00:00:00.000Z - min: 200, max: 250
Вас также могут заинтересовать другие агрегированные показатели, такие как Stats Agggregation