Aggregate Optimizely Graph results with the C# SDK
Use .Facet() to retrieve value counts for one or more fields alongside query results. Facets power category navigation, filter-by-author panels, and other UI patterns that require aggregated counts.
Retrieve facet counts
Request value counts for one or more fields to display in a filter panel or navigation sidebar.
var result = await client
.QueryContent<BlogPostPage>()
.Facet(x => x.Author!)
.Facet(x => x.ViewCount!)
.GetAsContentAsync();Read the counts back from the result:
var authorFacet = result.Facets?.GetFacet(x => x.Author);
var viewCountFacet = result.Facets?.GetFacet(x => x.ViewCount);Each facet entry contains a value and its count.
Use facets with full-text search
Facet counts reflect the current search results, not the full content set:
var result = await client
.QueryContent<BlogPostPage>()
.SearchFor("api")
.UsingFullText()
.Facet(x => x.ViewCount)
.GetAsContentAsync();Filter within facets
Pass filters to apply a value filter while keeping the full facet list. This pattern is essential for faceted navigation. Users refine or change their selection without losing the other options.
var result = await client
.QueryContent<BlogPostPage>()
.Facet(x => x.Author, filters: ["John Developer"])
.GetAsContentAsync();The result set is filtered to posts by John Developer, but the facet still shows counts for all authors so the user can switch.
Multi-select facets
To let users select multiple values within a single facet, use OR logic. Selections across multiple facets combine using AND logic. Pass an empty string ("") as a placeholder for facets with no active selection:
var result = await client
.QueryContent<BlogPostPage>()
.Facet(x => x.BlogCategory!, filters: [""])
.Facet(x => x.Author!, filters: ["John Developer"])
.GetAsContentAsync();
var categoryFacet = result.Facets?.GetFacet(x => x.BlogCategory);
var authorFacet = result.Facets?.GetFacet(x => x.Author);