Written by The Jahia Team
 
Developers
   Estimated reading time:

Facets allow you to see groupings based on similar or exact characteristics of a property. For example, you can see how many documents were created by each user or how many documents fall in a given creation date range. 

Similar to filter, you can use term, numberRange and dateRange objects to specify the type of facet you require. Each facet allows you to specify field, whether it is disjunctive or not, and selections. The selection object varies depending on the type of facet. Here’s how term and date range facets look in a query. Note that date math expressions are fully supported. 

{
  jcr {
    searches(siteKey: "digitall", language: "en", workspace: LIVE) {
      search(..., 
             sortBy:{...},
           filter:{...},
        facets:{
                term:[{field:"jcr:createdBy"}],
                  dateRange: [{field: "jcr:lastModified", 
                    ranges: [{from: "now-1M", to: "now", 
                              name: "last month"}, 
                             {from: "now-1y", to: "now", 
                              name: "last year"}]}]
          })  {
       ...
      }
    }
  }
}

To correctly retrieve values for your facets, you must specify which value you want to resolve: TermValue or DateRangeValue.

facets {
  field
  data {
    ...on TermValue {
      value
      count
    }
    ...on DateRangeValue {
      range {
        from
        to
        name
      }
     count
    }
  }
}

To filter on facets, you need to specify one or more selections in the selections list. For term facets, selections is a list of strings.

term:[{field:"jcr:createdBy" selections:["root"]}]

For range facets, it is an object describing the range. 

dateRange: [{field: "jcr:lastModified", 
              ranges: [{from: "now-1M", to: "now", name: "last month"}, 
                        {from: "now-1y", to: "now", name: "last year"}] 
              selections:[{from: "now-1M", to: "now", name: "last month"}]}]

In this example, the set of results contains only the documents that were created by root and match the modified date criteria.

That’s it, full text search API is that simple.