Building Search API with ElasticSearch — Part 06: Filters

André Coelho
3 min readJul 11, 2022

--

In the previous post we created facets to refine the search results and to finish this work we will use the filters.

The filter is a clause that will say whether or not the document matches the criterion and it does not affect the document’s score.
Another important point is that the filters are cached and this optimizes the query performance.

Java Code

To create the filters, we will define in the SearchFilter the fields that will receive the parameters. The fields selected for filtering will be “genre” and “certificate”.

These filters became a Map of filters because we will pass this Map to the SearchRequest.
To add the filter it is necessary to use the Bool Query and it will be in this clause that we will use the Terms Query because we will use a list of terms for filtering.

Running test

The test will be with the term “batman”, the result will be a list with 4 movies.

curl --location --request GET 'http://localhost:8080/api/movies/search?size=10&text=batman'

Results:

{
"size": 10,
"total": 4,
"suggestion": "",
"movies": [
{
"title": "Batman Begins",
"certificate": "UA",
"genre": [
"Action",
"Adventure"
],
"search_after": [
94.20764,
156
]
},
{
"title": "Batman: Mask of the Phantasm",
"certificate": "PG",
"genre": [
"Animation",
"Action",
"Crime"
],
"search_after": [
79.15011,
666
]
},
{
"title": "The Dark Knight",
"certificate": "UA",
"genre": [
"Action",
"Crime",
"Drama"
],
"search_after": [
9.961502,
3
]
},
{
"title": "The Dark Knight Rises",
"certificate": "UA",
"genre": [
"Action",
"Adventure"
],
"search_after": [
9.7500925,
64
]
}
]
}

Executing the query that returns the facets we have the result below:

curl --location --request GET 'http://localhost:8080/api/movies/facets?text=batman'
Restults Facets

We want to refine the results, we select the genre “Crime”, we hope to have only two documents as results.

curl --location --request GET 'http://localhost:8080/api/movies/search?size=10&text=batman&genres=Crime'

Results:

Results filters genre Crime

Now let’s filter again by the “PAG” certificate.

curl --location --request GET 'http://localhost:8080/api/movies/search?size=10&text=batman&genres=Crime&certificates=PG'

And the result will be just 1 document.

Results filter by genre and certificate

We finish here, in the next article we will implement a term correction feature known as “did you mean”.

Building Search API with ElasticSearch — Part 01: Preparing the environment
Building Search API with ElasticSearch — Part 02: Pagination
Building Search API with ElasticSearch — Part 03: Autocomplete
Building Search API with ElasticSearch — Part 04: Search Query
Building Search API with ElasticSearch — Part 05: Simple Facets
Building Search API with ElasticSearch — Part 06: Filters
Building Search API with ElasticSearch — Part 07: “Did you mean”
Building Search API with ElasticSearch — Part 08: Sort results
Building Search API with ElasticSearch — Part 09: Recommendation with More Like This Query
Building Search API with ElasticSearch — Part 10: Front-End
Building Search API with ElasticSearch — Part 11: Elastic APM

--

--

André Coelho

Developer of web and mobile systems. Enthusiast in the area of ​​automation and electronics and I have hobbie music.