Thursday, February 27, 2025

Apache Solr v9 Range Faceting Tutorial

Apache Solr v9 Range Faceting Tutorial

Solr Documentation Link


This tutorial does not cover all Range Facet options. Refer to the document link above for full details. 

This tutorial will cover some of the facet options along with visual examples to help solidify your understanding.


This is the subset of parameters that are explained in this tutorial.


Parameter

Description

field

The numeric field or date field to produce range buckets from.

start

Lower bound of the ranges.

end

Upper bound of the ranges.

gap

Size of each range bucket produced.

include

By default, the ranges used to compute range faceting between start and end are 

 inclusive of their lower bounds and exclusive of the upper bounds. This default, equivalent to 

"lower" below, will not result in double counting at the boundaries. The include parameter 

may be any combination of the following options:

  • "lower" all gap based ranges include their lower bound

  • "upper" all gap based ranges include their upper bound

  • "edge" the first and last gap ranges include their edge bounds (i.e., lower for 

    the first one, upper for the last one) even if the corresponding upper/lower option 

    is not specified


This tutorial is based on code that uses SolrJ and the RangeFacetMap. Here is an example of code.


RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1910,

                                                1950,

                                                3);



//Note: If additional parameters are needed, put them directly into the facet map

//by key according to documentation.

// https://solr.apache.org/guide/solr/latest/query-guide/faceting.html

//

// Example: rangeFacetMap.put("hardened",true)

// Example: rangeFacetMap.put("include",Make.listOf("upper"))

// Example: rangeFacetMap.put("other", "after")



Facet Data

The example data is a birth year field and the count of records that have that birth year. 


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2


Gap Size 1

RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1900,

                                                1907,

                                                1);


Each bucket is represented by a unique color.


Lower

All gap based ranges include their lower bound.


Start = 1900, End = 1907, Gap = 1, include = “lower”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

1

1901

2

1902

3

1903

1

1904

2

1905

3

1906

1


Upper

All gap based ranges include their upper bound.


Start = 1900, End = 1907, Gap = 1, include = “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

2

1901

3

1902

1

1903

2

1904

3

1905

1

1906

2


Edge

The first and last gap ranges include their edge bounds (i.e., lower for the first one, upper for the last one) 

even if the corresponding upper/lower option is not specified.


Start = 1900, End = 1907, Gap = 1, include = “edge”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

1

1901

0

1902

0

1903

0

1904

0

1905

0

1906

2


UPPER and LOWER


Start = 1900, End = 1907, Gap = 1, include = “lower”, “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

3

1901

5

1902

4

1903

3

1904

5

1905

4

1906

3



Gap Size 2

RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1900,

                                                1907,

                                                2);



Each bucket is represented by a unique color.


Lower

All gap based ranges include their lower bound.


Start = 1900, End = 1907, Gap = 2, include = “lower”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

3

1902

4

1904

5

1906

3


Upper

All gap based ranges include their upper bound.


Start = 1900, End = 1907, Gap = 2, include = “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

5

1902

3

1904

4

1906

5



Edge

The first and last gap ranges include their edge bounds (i.e., lower for the first one, upper for the last one) 

even if the corresponding upper/lower option is not specified.


Start = 1900, End = 1907, Gap = 2, include = “edge”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

3

1902

1

1904

3

1906

2


UPPER and LOWER


Start = 1900, End = 1907, Gap = 2, include = “lower”, “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

6

1902

6

1904

6

1906

6





Gap Size 3

RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1900,

                                                1907,

                                                3);



Each bucket is represented by a unique color.


Lower

All gap based ranges include their lower bound.


Start = 1900, End = 1907, Gap = 3, include = “lower”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

6

1903

6

1906

6


Upper

All gap based ranges include their upper bound.


Start = 1900, End = 1907, Gap = 3, include = “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

6

1903

6

1906

6




Edge

The first and last gap ranges include their edge bounds (i.e., lower for the first one, upper for the last one) 

even if the corresponding upper/lower option is not specified.


Start = 1900, End = 1907, Gap = 3, include = “edge”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

6

1903

5

1906

5




Gap Size 4

RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1900,

                                                1907,

                                                4);



Each bucket is represented by a unique color.


Lower

All gap based ranges include their lower bound.


Start = 1900, End = 1907, Gap = 4, include = “lower”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

7

1904

8


Upper

All gap based ranges include their upper bound.


Start = 1900, End = 1907, Gap = 4, include = “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

8

1904

9




Edge

The first and last gap ranges include their edge bounds (i.e., lower for the first one, upper for the last one) 

even if the corresponding upper/lower option is not specified.


Start = 1900, End = 1907, Gap = 4, include = “edge”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

7

1904

6




Gap Size 5

RangeFacetMap rangeFacetMap = new RangeFacetMap("BirthYears",

                                                1900,

                                                1907,

                                                5);



Each bucket is represented by a unique color.


Lower

All gap based ranges include their lower bound.


Start = 1900, End = 1907, Gap = 5, include = “lower”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

9

1905

10


Upper

All gap based ranges include their upper bound.


Start = 1900, End = 1907, Gap = 5, include = “upper”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

11

1905

9




Edge

The first and last gap ranges include their edge bounds (i.e., lower for the first one, upper for the last one) 

even if the corresponding upper/lower option is not specified.


Start = 1900, End = 1907, Gap = 5, include = “edge”


1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

3

1

2

3

1

2

3

1

2

3

1

2

3

1

2



Value

Count

1900

9

1905

7