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).
}
2 回答
走向分面确实是要走的路:
http://www.elasticsearch.org/guide/reference/api/search/facets/date-histogram-facet/
你想要的是date_histogram-facet .
这是一个应该涵盖我可以解释你想要的各种方式的例子: