🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Methods / Search

Search for facet values

Required API Key: any key with the search ACL
Method signature
$index->searchForFacetValues(string facetName, string facetQuery)
$index->searchForFacetValues(string facetName, string facetQuery, [
  // any searchParameters
  // any requestOptions
])

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

Search for a set of values within a given facet attribute. Can be combined with a query.

This method lets you search through the values of a facet attribute and select a subset of those values that meet a given criteria.

Facet-searching only affects facet values, not the index search.

For a facet attribute to be searchable, it must have been declared in the attributesForFaceting index setting using the searchable() modifier.

By default:

  • Results are sorted by decreasing count. You can adjust with sortFacetValuesBy.
  • A maximum of 10 results are returned. You can adjust with maxFacetHits.

This method is often used in combination with a user’s current search with search parameters. By combining facet and query searches, you control the number of facet values a user sees.

Api method search for facet values

When you have thousands of different values for a given facet attribute, it’s impossible to display them all on a user interface. With Search for facet values, you allow your users to search within a specific faceted attribute (for example, brands, authors, or categories) without needing to create a separate index. You can still display the most common occurrences for each facet at the top, but also enable the user to search for a specific value for filtering.

By default, facet values are sorted by their count.

Search for facet values doesn’t work if you have more than 65 searchable facets and searchable attributes combined: any such search results in an empty list of facet values.

Examples

Read the Algolia CLI documentation for more information.

Search for facet values

1
2
# Search the values of the "category" facet matching "phone".
$index->searchForFacetValues("category", "phone");

Search for facet values with additional search parameters

1
2
3
4
5
// Search the "category" facet for values matching "phone"
// in records having "Apple" in their "brand" facet.
$index->searchForFacetValues("category", "phone", [
  'filters' => 'brand:Apple'
]);

Search for facet values and send extra HTTP headers

1
2
3
4
# Search the "category" facet for values matching "phone" in records
$index->searchForFacetValues("category", "phone", [
  'X-Algolia-User-ID' => 'user123'
]);

Parameters

Parameter Description
facetName
type: string
Required

Attribute name.

For this to work, you must declare the attribute with attributesForFaceting using the searchable() modifier.

facetQuery
type: string
Required

The search query used to search the facet attribute.

Facet queries only match prefixes, typos, and exact.

searchParameters
type: key-value mapping
default: No search parameters
Optional

The mapping of search parameters used to search the underlying index.

If set, the method will return facet values that both:

  • Match the facet query
  • Are contained in the records matching the regular search query

Using this parameter aligns the count of each facet value to current search results. Without it, the count is based on the whole index.

page, hitsPerPage, offset, and length parameters don’t affect the count, as the count value represents the whole set of results, not just the current page. maxFacetHits and sortFacetValuesBy do affect the returned facet values.

requestOptions
type: key/value mapping
default: No request options
Optional

A mapping of requestOptions to send along with the query. In addition to sending extra HTTP headers or setting timeouts, you can use requestOptions to specify search parameters with this method.

Response

This section shows the JSON response returned by the API. Each API client encapsulates this response inside objects specific to the programming language, so that the actual response might be different. You can view the response by using the getLogs method. Don’t rely on the order of attributes in the response, as JSON doesn’t guarantee the ordering of keys in objects.

JSON format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
    "facetHits": [
        {
            "value": "Mobile phones",
            "highlighted": "Mobile <em>phone</em>s",
            "count": 507
        },
        {
            "value": "Phone cases",
            "highlighted": "<em>Phone</em> cases",
            "count": 63
        }
    ],
    "exhaustiveFacetsCount": true,
    "exhaustive": {
      "facetsCount": true
    },
    "processingTimeMS": 1
}
Field Description
facetHits
list of facetHit
exhaustiveFacetsCount
boolean

Whether the count returned for each facetHit is exhaustive.

processingTimeMS
integer

Processing time.

facetHit

Field Description
value
string

Facet value.

highlighted
string

Highlighted value.

count
integer

The number of times the value is present in the dataset.

Did you find this page helpful?