AggregationContainer 与 AggregationDescriptor
AggregationContainer vs. AggregationDescriptor
我正在尝试将范围数组发送到聚合描述符,但 Lambda 表达式需要逗号分隔的表达式
.Aggregations(agg =>
{
AggregationDescriptor ag = agg.Terms("objectTypes", ot => ot.Field("doc.objectType"));
if (!parameters.ContainsKey("userID"))
ag = ag.Terms("users", ot => ot.Field("doc.entryUserID"));//.Field("doc.sourceUserID")))
ag.Terms("commentTypes", ot => ot.Field("doc.commentType"));
if (!parameters.ContainsKey("dateRange"))
{
Dictionary<string, SearchDateRange> dateMap = GetDateRangeMap();
ag.DateRange("dates", dr => dr.Field("doc.date").Format("yyyy-MM-dd")
.Ranges(r1 => r1.Key("Today").From(dateMap["Today"].startDate.Value.ToString("yyyy-MM-dd")).To("now"),
r2 => r2.Key("SinceWednesday").From(dateMap["Today"].startDate.Value.ToString("yyyy-MM-dd")).To("now"),
r3 => r3.Key("ThisYear").From(dateMap["ThisYear"].startDate.Value.ToString("yyyy-MM-dd")).To("now"),
r3 => r3.Key("Last2Years").From(dateMap["Last2Years"].startDate.Value.ToString("yyyy-MM-dd")).To("now"),
r4 => r4.Key("Last3Years").From(dateMap["Last3Years"].startDate.Value.ToString("yyyy-MM-dd")).To("now")
));
}
以上代码有效。
下面我想使用 Range[] 数组并将其传递给聚合描述符,但我不能,但我可以使用范围数组创建一个 AggregationContainer。如何将这两部分结合在一起?
if (!parameters.ContainsKey("revenueRange") && docTypes.Contains(CouchbaseDocumentType.Company))
{
Dictionary<string, SearchNumberRange> numMap = GetMoneyRangeMap();
Range<double>[] ranges = numMap.Select(m =>
{
var r = new Range<double>().Key(m.Key);
if (m.Value.low.HasValue) r.From(m.Value.low.Value);
if (m.Value.high.HasValue) r.To(m.Value.high.Value);
return r;
}).ToArray();
AggregationContainer agr = new AggregationContainer
{
Range = new RangeAggregator { Field = "doc.lastFinancial.revenueUSD", Ranges = ranges }
};
}
return ag;
}
)
我创建了一个简单的示例来向您展示如何实现这一点。
var funcs = new List<Func<Range<double>, Range<double>>>();
funcs.Add(range => new Range<double>().From(1).To(2));
funcs.Add(range => new Range<double>().From(3).To(4));
var searchResponse = client.Search<Document>(
s => s.Aggregations(agg => agg.Range("range", descriptor => descriptor.Field(f => f.Number).Ranges(funcs.ToArray()))));
文件class:
public class Document
{
public int Id { get; set; }
public double Number { get; set; }
}
希望您将它放入您的上下文中不会有问题。
我正在尝试将范围数组发送到聚合描述符,但 Lambda 表达式需要逗号分隔的表达式
.Aggregations(agg => { AggregationDescriptor ag = agg.Terms("objectTypes", ot => ot.Field("doc.objectType"));
if (!parameters.ContainsKey("userID")) ag = ag.Terms("users", ot => ot.Field("doc.entryUserID"));//.Field("doc.sourceUserID"))) ag.Terms("commentTypes", ot => ot.Field("doc.commentType")); if (!parameters.ContainsKey("dateRange")) { Dictionary<string, SearchDateRange> dateMap = GetDateRangeMap(); ag.DateRange("dates", dr => dr.Field("doc.date").Format("yyyy-MM-dd") .Ranges(r1 => r1.Key("Today").From(dateMap["Today"].startDate.Value.ToString("yyyy-MM-dd")).To("now"), r2 => r2.Key("SinceWednesday").From(dateMap["Today"].startDate.Value.ToString("yyyy-MM-dd")).To("now"), r3 => r3.Key("ThisYear").From(dateMap["ThisYear"].startDate.Value.ToString("yyyy-MM-dd")).To("now"), r3 => r3.Key("Last2Years").From(dateMap["Last2Years"].startDate.Value.ToString("yyyy-MM-dd")).To("now"), r4 => r4.Key("Last3Years").From(dateMap["Last3Years"].startDate.Value.ToString("yyyy-MM-dd")).To("now") )); }
以上代码有效。
下面我想使用 Range[] 数组并将其传递给聚合描述符,但我不能,但我可以使用范围数组创建一个 AggregationContainer。如何将这两部分结合在一起?
if (!parameters.ContainsKey("revenueRange") && docTypes.Contains(CouchbaseDocumentType.Company)) { Dictionary<string, SearchNumberRange> numMap = GetMoneyRangeMap(); Range<double>[] ranges = numMap.Select(m => { var r = new Range<double>().Key(m.Key); if (m.Value.low.HasValue) r.From(m.Value.low.Value); if (m.Value.high.HasValue) r.To(m.Value.high.Value); return r; }).ToArray(); AggregationContainer agr = new AggregationContainer { Range = new RangeAggregator { Field = "doc.lastFinancial.revenueUSD", Ranges = ranges } }; } return ag; } )
我创建了一个简单的示例来向您展示如何实现这一点。
var funcs = new List<Func<Range<double>, Range<double>>>();
funcs.Add(range => new Range<double>().From(1).To(2));
funcs.Add(range => new Range<double>().From(3).To(4));
var searchResponse = client.Search<Document>(
s => s.Aggregations(agg => agg.Range("range", descriptor => descriptor.Field(f => f.Number).Ranges(funcs.ToArray()))));
文件class:
public class Document
{
public int Id { get; set; }
public double Number { get; set; }
}
希望您将它放入您的上下文中不会有问题。