Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Introduction

Histogram facets can be used on numerical and date fields to retrieve the number of documents whose value for the field falls into an interval. An example could be in the context of searching for products. We could then use a histogram facet to retrieve the number of products whose price range from 0 to 100, 101 to 200 etc.

Examples

Although the type of the interval parameter is different histogram facets both for numerical and date fields can be requested using the HistogramFacetFor method. It requires an expression which is used to determine what field to build the histogram for as the first parameter. As a second parameter it requires an interval which is either a numerical value or a value of the enum type DateInterval (located in the EPiServer.Find.Api.Facets namespace). Once the search has been executed histogram facets can be extracted from the search results object using a method with the same name, HistogramFacetFor.

Let us say we are searching for blog posts and want to display the number of posts that match the search segmented by in which month they were published.

C#
//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)];
Do you find this information helpful? Please log in to provide feedback.

Last updated: Apr 03, 2014

Recommended reading