You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to perform a faceted search?

To execute facet query using session Query method use ToFacets extension. There is also a possibility to convert query straight into FacetQuery instance using 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];