Facets selection
Select facet by field.
The Optimizely Graph .Net Client supports faceting or aggregating documents using the Facet
method. You must specify the field or fields you want to facet.
Beta
The Optimizely Graph .NET Client is in beta. Contact your Customer Success Manager for information.
The following is an example of a simple "facet" that facets the documents based on the value of the FacetProperty
field:
var query = queryBuilder
.ForType<MyDocumentType>()
.Facets(x=>x.FacetProperty)
.ToQuery()
.BuildQueries();
Facet on IEnumerable property
For the IEnumerable<T>
property:
- If
T
is a simple type (int, long, float, double, string, bool) or DateTime just use as a normal field. - If
T
is a complex type object. Use first expression for selectingIEnumerable
property then properties ofT
.
var query = queryBuilder
.ForType<MyDocumentType>()
.Facets(x=>x.MyEnumerableProp, f=> f.FacetProp)
.BuildQueries();
Alternatively, the most efficient way for dealing with IEnumberable<T>
where T
is a complex type is create an extension method for pretending property type IEnumberable<T>
to be T
. With that you can use all properties of type T
when select, filter, or facet using a simple block of code. Ensure that the method name equals with property name.
public static class CmsModelsExtension
{
...
public static ContentLanguageModel ExistingLanguages(this Content myprop)
{
return null;
}
}
//now you want to select properties in ExistingLanguages field, just use method ExistingLanguages()
// instead of property ExistingLanguages :
query.Fields(x=> x.ExistingLanguages().Name, x.ExistingLanguages().DisplayName)
//facets
query.Facet(x=> x.ExistingLanguages().Name.FacetLimit(10))
See Tool tips: Optimizely Graph Client Tool and how to leverage CMS data models to build query blog for more information.
Facet projection
By default, the result of the facet contains a Name
and Count
field. Use the Projection
method on IFilterOperator
to get your desired field.
var stringFacetFilter = new StringFacetFilterOperator()
.Filters(“value1”,”value2”)
.Limit(10)
.OrderBy(x=>x.Property1, OrderMode.DESC)
.Projection(FacetProperty.Name); //get only facet name
var query = queryBuilder
.ForType<NewsPage>()
.Facet(x=>x.Title, stringFacetFilter)
.ToQuery()
.BuildQueries();
Updated about 2 months ago