【发布时间】:2021-01-16 05:42:36
【问题描述】:
我是 ElasticSearch 的新手,我正在尝试进行聚合,但似乎无法正确完成。
我在 ElasticSearch 索引中有一些数据,如下所示:
{
"customerId": "example_customer",
"request": {
"referer": "https://example.org",
}
"@timestamp": "2020-09-29T14:14:00.000Z"
}
我的映射:
{
"mappings": {
"properties": {
"customerId": { "type": "keyword" },
"request": {
"properties": {
"referer": { "type": "keyword" }
}
}
}
}
}
我正在尝试获取在某个日期范围内针对特定客户出现频率最高的引荐来源网址。我可以像这样为客户制作过滤器:
var result = await _client.SearchAsync<InsightRecord>(s =>
s.Aggregations(
a => a
.Filter("customer", customer =>
customer.Filter(q => q.Term(ir => ir.CustomerId, customerId)))
.Terms("top_referer", ts => ts.Field("request.referer"))
)
);
return result.Aggregations.Terms("top_referer").Buckets
.Select(bucket => new TopReferer { Url = bucket.Key, Count = bucket.DocCount ?? 0})
现在我想将其缩小到特定的时间范围。这是我目前所拥有的:
var searchDescriptor = s.Aggregations(a =>
a.Filter("customer", customer =>
customer.Filter(q =>
q.Bool(b =>
b.Must(
f2 => f2.DateRange(date => date.GreaterThanOrEquals(from).LessThanOrEquals(to)),
f1 => f1.Term(ir => ir.CustomerId, customerId)
)
)
)
)
.Terms("top_referers", ts => ts.Field("request.referer"))
);
问题是日期过滤器没有包含在查询中,它转换为这个 JSON:
{
"aggs": {
"customer": {
"filter": {
"bool": {
"filter": [{
"term": {
"customerId": {
"value": "example_customer"
}
}
}
]
}
}
},
"top_referers": {
"terms": {
"field": "request.referer"
}
}
}
}
我尝试以不同的方式订购它们,但没有帮助。在 JSON 中显示的始终是客户过滤器,并且会跳过日期范围。我还看到有些人使用结合聚合的查询,但我觉得我应该能够单独使用聚合来做到这一点。这可能吗?我在查询中做错了什么,范围没有显示在 JSON 中?
【问题讨论】:
标签: c# json elasticsearch nest