This content is retired. See latest version here.

Last updated: Sep 26 2013


Range facets group documents based on what range a numeric or DateTime field falls into. Unlike histogram facets, the ranges do not have to be in an interval, such as 0-10, 10-20 etc but can instead be of different sizes and overlap each other, such as 0-10, 5-20. Another difference compared to histogram facets is that range facets return more information than the number of documents in each range, such as minimum and maximum value and mean value.


Range facets are requested using the RangeFacetFor method. Its first parameter is an expression which is used to select the property to group documents by. The second parameter is an array (params) of either NumericRange or DateRange objects depending on the type of the property. Both range types have two properties named From and To of type nullable double and nullable DateTime respectively.

Once a search request with one or more range facet requests have been executed range facets can be retrieved from the result object using a method with the same name used for requesting it, RangeFacetFor. This method however only have a single parameter, an expression saying what property the range facet was for.

One important thing to note is that ranges are inclusive for the lower bound and exclusive for the upper bound. That is, a range from 0 to 10 will match 0 but not 10. The same goes for date ranges where a range from 2012-01-01 to 2012-01-31 will match 2012-01-01 but not 2012-01-31.

Below is an example usage of range facets using it to group products into three different price ranges.

var priceRanges = new List<NumericRange>()
        new NumericRange { To = 100 },
        new NumericRange { From = 100, To = 300 },
        new NumericRange { From = 300 }

var searchResult = client.Search<Product>()
    .RangeFacetFor(x => x.Price, priceRanges.ToArray())

In the above example we create a number of ranges and request a range facet for products falling into those ranges. Note that in the first range we do not specify the From property effectively saying that this range is for all products with a price lower than 100. Similarly the third range only specifies to To property requesting all products that cost 300 or more.

Once we have gotten our search result back we can retrieve and inspect the facet. Below is an example of doing this, writing information about each range to the console.

var priceFacet = searchResult.RangeFacetFor(x => x.Price);

foreach (NumericRangeResult range in priceFacet)
    if (range.From.HasValue)
        Console.Write(" from " + range.From.Value);
    if (range.To.HasValue)
        Console.Write(" to " + range.To.Value);
    Console.WriteLine("Count: " + range.Count);
    Console.WriteLine("Min: " + range.Min);
    Console.WriteLine("Max: " + range.Max);
    Console.WriteLine("Mean: " + range.Mean);
    Console.WriteLine("Total: " + range.Total);

The above code produces output such as this:

Range to 100
Count: 61
Min: 0,5
Max: 97,3
Mean: 42,3237045467812
Total: 2581,74597735365

Range from 100 to 300
Count: 52
Min: 100,0
Max: 299,0
Mean: 205,738167025743
Total: 10698,3846853387

Range from 300
Count: 51
Min: 301,5
Max: 942,8
Mean: 566,562194859469
Total: 28894,6719378329