HomeDev guideRecipesAPI Reference
Dev guideUser GuidesLegal TermsNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Facet filter

How to filter on facets.

👍

Beta

The Optimizely Graph .NET Client is in beta. Contact your Customer Success Manager for information.

Different types of fields have different operators. There are three implementations of IFacetOperator interface:

  1. DateFacetFilterOperator – This class supports filtering for the DateTime field using the Unit and Value methods to set DateUnit.

    • Defaults:

      • Unit – DAY
      • Value – 1
    • You can retrieve the facets of date fields as a histogram.

    • The following example will bucket the documents with the Created field by 14 days:

      var dateFacetFilter = new DateFacetFilterOperator()
        .Unit(DateUnit.DAY)
        .Value(14);
      
      var query = queryBuilder
      .ForType<NewsPage>()
        .Facet(x=>x.Created, dateFacetFilter)
      .ToQuery()
      .BuildQueries();
      
      var result = await query.GetResultAsync();
      var facets = result.GetContent<NewsPage>().Content.Facets.Values;
      
      
  2. NumericFacetFilterOperator – This class supports filtering in ranges for numeric fields. The parameters for the Ranges method are an array of tuples with values of [from] and [to].

    • In the following example, you create several ranges and request a range facet for products falling into those ranges. In the first range, you specify the [from] value to null, effectively saying that the range is for products with a price below 10. Similarly, the third range specifies the [to] value to null, requesting products that cost 300 or more.

      var numericFacetFilter = new NumericFacetFilterOperator().Ranges((null,10),(100,200),(300, null));
      var query = queryBuilder
      .ForType<MyProduct>()
        .Facet(x=>x.Price, numericFacetFilter)
      .ToQuery()
      .BuildQueries();
      
      
  3. StringFacetFilterOperator – This class supports filters on string and boolean field types:

    • Filters
    • Limit
    • OrderType
    • OrderBy
    var stringFacetFilter = new StringFacetFilterOperator()
      .Filters(“value1”,”value2”)
      .Limit(10)
      .OrderBy(x=>x.Property1, OrderMode.DESC)
      .OrderType(OrderType.COUNT);
    
    var query = queryBuilder
    .ForType<NewsPage>()
      .Facet(x=>x.Title, stringFacetFilter)
    .ToQuery()
    .BuildQueries();
    
    var facets = await query.GetResultAsync<NewsPage>().Content.Facets;
    

    For details, see the Facet order and Facet limit sections.

Facet filters

The facet filter specifies the facets that should be applied to the field. It is a list of string values.

The facet filter supports the multi-selection of facets. For example, you can apply a facet as a filter to update the list of results while preserving the list of original facets and getting an updated hit count for the items and facets without filters.

📘

Note

If the value of filters is an empty string "", it is ignored.

In the Optimizely Graph .NET Client, use the FacetFilters method in EPiServer.ContentGraph.Extensions to create facet filters for your query.

var query = queryBuilder
.ForType<NewsPage>()
  .Facet(x=>x.ContentType.FacetFilters("sample"))
.ToQuery()
.BuildQueries();

Facet order

The OrderBymethod in the facet filter operator sets the ordering of the results. The OrderMode can be ASC (ascending) or DESC (descending).

  • OrderMode – Specify ASC or DESC ordering of the facets. If you do not specify an OrderMode, the default is DESC.
  • OrderType – By COUNT or by VALUE. If you do not specify OrderType, the default is COUNT.

You can use FacetOrder method in EPiServer.ContentGraph.Extensions to create facet order for your query.

var query = queryBuilder
.ForType<NewsPage>()
  .Facet(x=>x.ContentType.FacetOrder(OrderType.NAME, OrderMode.DESC))
.ToQuery()
.BuildQueries();

Facet limit

The Limit method limits the result of the facet. Specifies up to 1000 facets to retrieve. The default is 10.

With EPiServer.ContentGraph.Extensions:

var query = queryBuilder
.ForType<NewsPage>()
  .Facet(x=>x.ContentType.FacetLimit(5))
.ToQuery()
.BuildQueries();

Facet filters on IEnumerable property

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)  
//filters  
query.Where(x=> x.ExistingLanguages().Name.StartWith("e"))  
//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.

Another option is, if your property has type of IEnumerable<T> where T is a complex type, and you want to facet by a property of T, see the following example:

var query = queryBuilder
.ForType<NewsPage>()
  .Facet(x=>x.EnumProperty, x=> x.FacetProperty.FacetFilters("test1", "test2"))
.ToQuery()
.BuildQueries();