Facets

November 14, 2023

Principle

Facets can be used on indexed properties (see Indexing nodetype properties). The principle is that the initial query will return the list of facets, and the number of matching results. And when a facet value is selected, then it is passed to the query as filter, and the query is then re-executed updating the result list.

Facet types

termFacet

The termFacet aggregates documents for each unique value of a specified field. For example, the query below returns the number of documents per author, as long as an author has created more than 3 content.

query {
  search(q: "searched terms", minDocCount: 3) {
    termFacet(field: "jgql:createdBy") {
      data {
        count
        value
      }
    }
  }
}

 

Argument Description Default value
field (mandatory)   NA
max Maximum number of facet groups returned. 10
minDocCount Minimum number of documents for a value to return as a facet. 1
missingValue Label to be used for missing values on this field. If not specified (default behavior), missing fields will not be counted -
disjunctive   false

treeFacet - Used to facet categories

The treeFacet node provides a very similar feature but supports hierarchical use cases. This is the facet type to use for categories. 

Argument Description Default value
field (mandatory)   NA
rootPath  (mandatory)   NA
max Maximum number of facet groups returned. 10
minDocCount Minimum number of documents for a value to return as a facet. 1
missingValue Label to be used for missing values on this field. If not specified (default behavior), missing fields will not be counted -
disjunctive   false

Available result fields:

Argument Description
count  
filter  
humanPath  
key  
rootPath  
value  
hasChildren  

rangeFacet

The rangeFacet creats data buckets based on user specified date or number ranges. Support for the date math expression allows you to create of buckets based on fixed dates or automatically generated from today's date.

The query below is an example mixing both fixed dates and date math expressions (although such query has a limited practical use).


query {
  search(q: "") {
    created: rangeFacet(
      field: "jcr:created"
      ranges: [
        { from: "2015-01-01", to: "2015-12-31", name: "2015" }        
        { from: "2016-01-01", to: "2016-12-31", name: "2016" }
        { from: "now-1y", to: "now", name: "Last year" }
      ]
    ) {
      data {
        name
        count
      }
    }
  }
}


This will produce the following result:


{
  "data": {
    "search": {
      "created": {
        "data": [
          {
            "name": "2015",
            "count": 0
          },
          {
            "name": "2016",
            "count": 137
          },
          {
            "name": "Last year",
            "count": 7
          }
        ]
      }
    }
  }
}

numberRange

The numberRange facet, given a numerical field, provides access to max and min values for that field across the entire dataset.