Loading...

Add genre and year filters

Let visitors narrow things down: “sci-fi only”, “made after 2005”.

What a facet is

A facet is a field you filter or group by rather than search inside: genre, year, rating, “in stock”. Search Stack counts how many results fall into each value, which fills the checklists down the side of a results page (“Sci-Fi (3), Action (1)”). Each facet has a type: a genre is a String, a year a Number, a release date a Date, an in-stock flag a Boolean.

Add genre and year

On the movies list, choose Fields then Add Facet Field. Add genre as a String, then year as a Number.

The Add Facet dialog with name 'genre' and type String

Edit each film to fill in its genre and year. The list now carries all four fields:

The movies list showing name, plot, genre and year columns

Filter a search

Facet values come back in every result, so a client can show “Sci-Fi · 2010” beside a film with no extra request. To restrict results, add a filter:

GET https://api.searchstack.dev/search/Demo/movies/1?query=&filter=genre eq 'Sci-Fi' and year gt 2005
X-API-Key: {your key}
{
  "results": [
    { "name": "Inception",     "fields": { "genre": "Sci-Fi", "year": 2010 } },
    { "name": "Interstellar",  "fields": { "genre": "Sci-Fi", "year": 2014 } }
  ],
  "count": 2,
  "total_count": 2
}

List a facet's values

To build a filter dropdown you need to know which values exist. The same read key the search endpoints use works here:

GET https://api.searchstack.dev/facet/Demo/movies/1/genre
X-API-Key: {your key}
["Sci-Fi", "Thriller", "Drama"]

The filter grammar

Text values go in single quotes; numbers and dates don't. Combine clauses with and, or and not.

OperatorMeansExample
eqequal togenre eq 'Action'
nenot equal togenre ne 'Horror'
gt / gegreater than / or equalyear gt 2005
lt / leless than / or equalyear le 2000
andboth must holdgenre eq 'Sci-Fi' and year gt 2005
oreither can holdgenre eq 'Action' or genre eq 'Sci-Fi'
notreverses a clausenot (year lt 2000)

The same filter works on suggest, so your autocomplete box can respect a “Sci-Fi only” toggle as the visitor types.

Go deeper: Facet types, grouping and the full filter grammar in the reference.

Top