elasticsearch 使用NEST和ElasticSearch每天统计文档数
我正在尝试使用NEST实现ElasticSearch查询,以便能够生成键/值对的结果,其中键是日期,值是计数。我查询的所有文档都有一个创建日期,我想计算在过去7天或类似时间内每天插入的文档数量 我已经检查了IElasticClient的计数方法,但这似乎给了我一个总计数,而不是每天。我想我需要对数据做一个方面的研究,但我不太清楚如何实现它 任何帮助都将不胜感激:)您需要的是一个方面 以下是一个例子,其中应包括我可以解释您所需内容的各种方式:elasticsearch 使用NEST和ElasticSearch每天统计文档数,elasticsearch,nest,elasticsearch,Nest,我正在尝试使用NEST实现ElasticSearch查询,以便能够生成键/值对的结果,其中键是日期,值是计数。我查询的所有文档都有一个创建日期,我想计算在过去7天或类似时间内每天插入的文档数量 我已经检查了IElasticClient的计数方法,但这似乎给了我一个总计数,而不是每天。我想我需要对数据做一个方面的研究,但我不太清楚如何实现它 任何帮助都将不胜感激:)您需要的是一个方面 以下是一个例子,其中应包括我可以解释您所需内容的各种方式: export ELASTICSEARCH_ENDPOI
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type","_id":1}}
{"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
{"index":{"_index":"play","_type":"type","_id":2}}
{"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
{"index":{"_index":"play","_type":"type","_id":3}}
{"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
{"index":{"_index":"play","_type":"type","_id":4}}
{"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"facets": {
"foos_per_interval": {
"date_histogram": {
"key_field": "created_date",
"value_field": "count",
"interval": "day"
},
"facet_filter": {
"term": {
"key": "foo"
}
}
}
}
}
'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [ {
"_index" : "play",
"_type" : "type",
"_id" : "1",
"_score" : 1.0, "_source" : {"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "2",
"_score" : 1.0, "_source" : {"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "3",
"_score" : 1.0, "_source" : {"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "4",
"_score" : 1.0, "_source" : {"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
} ]
},
"facets" : {
"foos_per_interval" : {
"_type" : "date_histogram",
"entries" : [ {
"time" : 1380499200000,
"count" : 1,
"min" : 12.0,
"max" : 12.0,
"total" : 12.0,
"total_count" : 1,
"mean" : 12.0
}, {
"time" : 1380585600000,
"count" : 2,
"min" : 13.0,
"max" : 42.0,
"total" : 55.0,
"total_count" : 2,
"mean" : 27.5
} ]
}
}
}
从各个方面着手确实是一条路:
公共类文档
{
公共字符串Id{get;set;}
public DateTime CreatedOn{get;set;}
}
public void TempFacetExample()
{
var result=this.\u client.Search(s=>s
.FaceDateHistogram(fd=>fd
.OnField(p=>p.CreatedOn)
.Interval(DateInterval.Day)
//全局强制将其计算在范围之外
//来自主查询(如果有)
.Global()
.FaceFilter(ff=>ff
.范围(rf=>rf
.From(DateTime.UtcNow.AddDays(-7))
.To(DateTime.UtcNow)
)
)
)
);
var facetBucket=result.Facet(p=>p.CreatedOn);
//facetBucket.Items现在使用计数保存天数
//如果我没记错的话,elasticsearch不会返回空桶
//因此,您必须处理丢失的天数(没有文档)。
}
注意:该过滤器不可缓存。根据索引流和搜索模式,将特定于日期的索引划分为多个索引,只搜索相关日期的索引可能是一个好主意。我给了Martijn答案,因为问题是关于NEST的。但你的答案正是我想要的。非常感谢你!
public class Doc
{
public string Id { get; set; }
public DateTime CreatedOn { get; set; }
}
public void TempFacetExample()
{
var result = this._client.Search<Doc>(s => s
.FacetDateHistogram(fd => fd
.OnField(p => p.CreatedOn)
.Interval(DateInterval.Day)
//global forces it to count out of scope
//from the main query (if any)
.Global()
.FacetFilter(ff => ff
.Range(rf => rf
.From(DateTime.UtcNow.AddDays(-7))
.To(DateTime.UtcNow)
)
)
)
);
var facetBucket = result.Facet<DateHistogramFacet>(p => p.CreatedOn);
//facetBucket.Items now holds the days with counts
//if I remember correctly elasticsearch wont return empty buckets
//so you have to handle missing days (that have no docs).
}