Session: Querying: How to Perform a Faceted Search

To execute a facet query using the session Query method, use ToFacets extension. There is also a possibility to convert the query straight into the FacetQuery instance using the ToFacetQuery extension.

Syntax

FacetResults ToFacets<T>(
	this IQueryable<T> queryable,
	IEnumerable<Facet> facets,
	int start = 0,
	int? pageSize = null) { ... }

FacetResults ToFacets<T>(
	this IQueryable<T> queryable,
	string facetSetupDoc,
	int start = 0,
	int? pageSize = null) { ... }
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
FacetResults facets = session.Query<Camera>()
	.ToFacets(
		new List<Facet>
			{
				new Facet
					{
						Name = "Manufacturer"
					},
				new Facet
					{
						Name = "Cost_Range",
						Mode = FacetMode.Ranges,
						Ranges =
							{
								"[NULL TO Dx200.0]",
								"[Dx300.0 TO Dx400.0]",
								"[Dx500.0 TO Dx600.0]",
								"[Dx700.0 TO Dx800.0]",
								"[Dx900.0 TO NULL]"
							}
					},
				new Facet
					{
						Name = "Megapixels_Range",
						Mode = FacetMode.Ranges,
						Ranges =
							{
								"[NULL TO Dx3.0]", 
								"[Dx4.0 TO Dx7.0]", 
								"[Dx8.0 TO Dx10.0]", 
								"[Dx11.0 TO NULL]"
							}
					}
			});

TimeSpan duration = facets.Duration;
Dictionary<string, FacetResult> results = facets.Results;

Example II

// using predefined facet setup
session.Store(
	new FacetSetup
		{
			Facets =
				new List<Facet>
					{
						new Facet
							{
								Name = "Manufacturer"
							},
						new Facet
							{
								Name = "Cost_Range",
								Mode = FacetMode.Ranges,
								Ranges =
									{
										"[NULL TO Dx200.0]",
										"[Dx300.0 TO Dx400.0]",
										"[Dx500.0 TO Dx600.0]",
										"[Dx700.0 TO Dx800.0]",
										"[Dx900.0 TO NULL]"
									}
							},
						new Facet
							{
								Name = "Megapixels_Range",
								Mode = FacetMode.Ranges,
								Ranges =
									{
										"[NULL TO Dx3.0]", 
										"[Dx4.0 TO Dx7.0]", 
										"[Dx8.0 TO Dx10.0]", 
										"[Dx11.0 TO NULL]"
									}
							}
					}
		}, "facets/CameraFacets");

session.SaveChanges();

FacetResults facets = session
	.Query<Camera>("Camera/Costs")
	.ToFacets("facets/CameraFacets");

TimeSpan duration = facets.Duration;
Dictionary<string, FacetResult> results = facets.Results;

Converting Query into FacetQuery

FacetQuery ToFacetQuery<T>(
	this IQueryable<T> queryable,
	IEnumerable<Facet> facets,
	int start = 0,
	int? pageSize = null) { ... }

FacetQuery ToFacetQuery<T>(
	this IQueryable<T> queryable,
	string facetSetupDoc,
	int start = 0,
	int? pageSize = null) { ... }
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>()
	.ToFacetQuery("facets/CameraFacets1");

FacetQuery facetQuery2 = session.Query<Camera>()
	.ToFacetQuery("facets/CameraFacets2");

FacetResults[] results = session
	.Advanced
	.MultiFacetedSearch(facetQuery1, facetQuery2);

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