HomeDev guideAPI ReferenceGraphQL
Dev guideUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

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 filter

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();