see on GitHub

Session: Querying: How to perform a faceted search?

To execute facet query using session query method use toFacets method.

Syntax

public FacetResults toFacets(List<Facet> facets);

public FacetResults toFacets(List<Facet> facets, int start);

public FacetResults toFacets(List<Facet> facets, int start, Integer pageSize);

public FacetResults toFacets(String facetSetupDoc);

public FacetResults toFacets(String facetSetupDoc, int start);

public FacetResults toFacets(String facetSetupDoc, int start, Integer pageSize);

Parameters
facets List<Facet> List of facets required to perform a facet query (mutually exclusive with facetSetupDoc).
facetSetupDoc String Document key that contains predefined FacetSetup (mutually exclusive with facets).
start int number of results that should be skipped. Default: 0.
pageSize int maximum number of results that will be retrieved. Default: null.
Return Value
FacetResults Facet query results with query duration and list of results - one entry for each term/range as specified in [FacetSetup] document or passed in parameters.

Example I

// passing facets directly
Facet facet1 = new Facet();
facet1.setName("Manufacturer");

Facet facet2 = new Facet();
facet2.setName("Cost_Range");
facet2.setMode(FacetMode.RANGES);
facet2.setRanges(Arrays.asList("[NULL TO Dx200.0]",
                                  "[Dx300.0 TO Dx400.0]",
                                  "[Dx500.0 TO Dx600.0]",
                                  "[Dx700.0 TO Dx800.0]",
                                  "[Dx900.0 TO NULL]"));

Facet facet3 = new Facet();
facet3.setName("Megapixels_Range");
facet3.setMode(FacetMode.RANGES);
facet3.setRanges(Arrays.asList( "[NULL TO Dx3.0]",
                                  "[Dx4.0 TO Dx7.0]",
                                  "[Dx8.0 TO Dx10.0]",
                                  "[Dx11.0 TO NULL]"));

List<Facet> facets = Arrays.asList(facet1, facet2, facet3);

FacetResults facetsResult = session.query(Camera.class).toFacets(facets);
Map<String, FacetResult> results = facetsResult.getResults();

Example II

// using predefined facet setup
Facet facet1 = new Facet();
facet1.setName("Manufacturer");

Facet facet2 = new Facet();
facet2.setName("Cost_Range");
facet2.setMode(FacetMode.RANGES);
facet2.setRanges(Arrays.asList("[NULL TO Dx200.0]",
                                  "[Dx300.0 TO Dx400.0]",
                                  "[Dx500.0 TO Dx600.0]",
                                  "[Dx700.0 TO Dx800.0]",
                                  "[Dx900.0 TO NULL]"));

Facet facet3 = new Facet();
facet3.setName("Megapixels_Range");
facet3.setMode(FacetMode.RANGES);
facet3.setRanges(Arrays.asList( "[NULL TO Dx3.0]",
                                  "[Dx4.0 TO Dx7.0]",
                                  "[Dx8.0 TO Dx10.0]",
                                  "[Dx11.0 TO NULL]"));

List<Facet> facets = Arrays.asList(facet1, facet2, facet3);
session.store(new FacetSetup("facets/CameraFacets", facets));
session.saveChanges();

FacetResults facetResults = session.query(Camera.class, "Camera/Costs").toFacets("facets/CameraFacets");
Map<String, FacetResult> results = facetResults.getResults();

Converting Query into FacetQuery

public FacetQuery toFacetQuery(List<Facet> facets);

public FacetQuery toFacetQuery(List<Facet> facets, int start);

public FacetQuery toFacetQuery(List<Facet> facets, int start, Integer pageSize);

public FacetQuery toFacetQuery(String facetSetupDoc);

public FacetQuery toFacetQuery(String facetSetupDoc, int start);

public FacetQuery toFacetQuery(String facetSetupDoc, int start, Integer pageSize);

Parameters
facets List<Facet> List of facets required to perform a facet query (mutually exclusive with facetSetupDoc).
facetSetupDoc String Document key that contains predefined FacetSetup (mutually exclusive with facets).
start int number of results that should be skipped. Default: 0.
pageSize int maximum number of results that will be retrieved. Default: null.
Return Value
FacetQuery Instance of FacetQuery containing all options set in query. Can be used with multiFacetedSearch from advanced session operations or with commands directly.

Example

FacetQuery facetQuery1 = session.query(Camera.class)
  .toFacetQuery("facets/CameraFacets1");

FacetQuery facetQuery2 = session.query(Camera.class)
  .toFacetQuery("facets/CameraFacets2");

FacetResults[] results = session.advanced()
  .multiFacetedSearch(facetQuery1, facetQuery2);

FacetResults facetResults1 = results[0];
FacetResults facetResults2 = results[1];