如何在 NEST、C# 中为 date_histogram 按周编写等效查询
How to write the equivalent query in NEST,C# for the date_histogram by week
我需要在使用 NEST 时将以下查询转换为 c#。
"aggs": {
"number_of_weeks": {
"date_histogram": {
"field": "@timestamp",
"interval": "week"
}
}
}
在 Kibana 中,输出是
我写了以下查询,但它给了我零个桶,而在 Kibana 中它 return 许多结果在桶中
var query3 = EsClient.Search<doc>(q => q
.Index("SomeIndex")
.Size(0)
.Aggregations(agg => agg.DateHistogram("group_by_week", e => e.Field(p => p.timestamp) .Interval(DateInterval.Week)
)) ;
var resultquery3 = query3.Aggregations.DateHistogram("group_by_week");
在 vs studio 中输出是
问题很可能是
e => e.Field(p => p.timestamp)
不会序列化到 Elasticsearch 中的 "@timestamp"
字段。为此,您需要映射 it with an attribute on the POCO
public class Doc
{
[Date(Name = "@timestamp")]
public DateTime timestamp { get; set; }
}
或将其映射到 ConnectionSettings
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultMappingFor<Doc>(m => m
.PropertyName(e => e.timestamp, "@timestamp")
);
var client = new ElasticClient(settings);
或者,您可以简单地 pass a string to .Field()
,它隐式转换
.Field("@timestamp")
我需要在使用 NEST 时将以下查询转换为 c#。
"aggs": {
"number_of_weeks": {
"date_histogram": {
"field": "@timestamp",
"interval": "week"
}
}
}
在 Kibana 中,输出是
我写了以下查询,但它给了我零个桶,而在 Kibana 中它 return 许多结果在桶中
var query3 = EsClient.Search<doc>(q => q
.Index("SomeIndex")
.Size(0)
.Aggregations(agg => agg.DateHistogram("group_by_week", e => e.Field(p => p.timestamp) .Interval(DateInterval.Week)
)) ;
var resultquery3 = query3.Aggregations.DateHistogram("group_by_week");
在 vs studio 中输出是
问题很可能是
e => e.Field(p => p.timestamp)
不会序列化到 Elasticsearch 中的 "@timestamp"
字段。为此,您需要映射 it with an attribute on the POCO
public class Doc
{
[Date(Name = "@timestamp")]
public DateTime timestamp { get; set; }
}
或将其映射到 ConnectionSettings
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultMappingFor<Doc>(m => m
.PropertyName(e => e.timestamp, "@timestamp")
);
var client = new ElasticClient(settings);
或者,您可以简单地 pass a string to .Field()
,它隐式转换
.Field("@timestamp")