facetQuery method

Future<List<AlgoliaFacetValueSnapshot>> facetQuery(
  1. String facetName, {
  2. String params = '',
  3. String facetQuery = '',
  4. int maxFacetHits = 10,
})

FacetQuerys

Search for values of a given facet, optionally restricting the returned values to those contained in objects matching other search criteria.

When the query is successful, the HTTP response is a 200 OK. It contains values that match the queried text, and that are contained in at least one object matching the other search parameters.


NOTE Pagination is not supported. The page and hitsPerPage parameters will be ignored. By default, maximum 10 values are returned. This can be adjusted via maxFacetHits.

The response body contains the following fields:

  • facetHits (array): Matched values. Each hit contains the following fields:
    • value (string): Raw value of the facet
    • highlighted (string): Highlighted facet value
    • count (integer): How many objects contain this facet value. This takes into account the extra search parameters specified in the query. Like for a regular search query, the counts may not be exhaustive. See the related discussion.

Implementation

Future<List<AlgoliaFacetValueSnapshot>> facetQuery(
  String facetName, {
  String params = '',
  String facetQuery = '',
  int maxFacetHits = 10,
}) async {
  var response = await algolia._apiCall(
    ApiRequestType.post,
    'indexes/$encodedIndex/facets/${Uri.encodeFull(facetName)}/query',
    data: {
      'params': params,
      'facetQuery': facetQuery,
      'maxFacetHits': maxFacetHits,
    },
  );
  Map<String, dynamic> body = json.decode(response.body);
  if (!(response.statusCode >= 200 && response.statusCode < 500)) {
    throw AlgoliaError._(body, response.statusCode);
  }
  return (body['facetHits'] as List<dynamic>)
      .map((e) => AlgoliaFacetValueSnapshot._(algolia, index, e))
      .toList();
}