HomeDev GuideAPI Reference
Dev GuideAPI ReferenceLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Histogram facets

Describes how facets based on histograms are used in Optimizely Search & Navigation.

Facets group documents based on specific terms or date ranges. 

Use histogram facets with numerical and date fields to retrieve the number of documents whose field value falls within an interval. For example, in a search of products, use a histogram facet to retrieve the number of products whose price ranges from 0 to 100, 101 to 200, and so on.

Use the HistogramFacetFor method to request histogram facets for numerical and date fields.

  • The first parameter takes an expression to determine the field to build the histogram. For example, PublishDate.
  • The second parameter requires an interval, a numerical value, or a value of the enum type DateInterval (located in the EPiServer.Find.Api.Facets namespace). Optimizely Search & Navigation bases this parameter on the field defined in the first parameter.

After you execute the search, extract histogram facets from the search results object using the method with the same name, HistogramFacetFor.

After searching blog posts, you want to display the number of posts for each publication month.

//Perform the search with the facet request included
var result = client.Search<BlogPost>()
  .For("Bananas")
  .HistogramFacetFor(x => x.PublishDate, DateInterval.Month)
  .GetResult();

//Retrieve the histogram facet for publication dates
var publicationMonths = searchResult
  .HistogramFacetFor(x => x.PublishDate);

//Iterate over the facet extracting values
foreach(var interval in publicationMonths.Entries) {
  DateTime month = interval.Key;
  int count = interval.Count;
}

//Retrieve the count for a specific month
int ? januaryCount = publicationMonths[new DateTime(2010, 12, 1)];