Building Search API with ElasticSearch — Part 05: Simple Facets

André Coelho
3 min readJul 9, 2022

--

You may have already noticed that in many search engines such as marketplaces there is a component that groups document characteristics and serves as a filter to refine the search results, this component is called Facets.

Facets Results Product

Let’s create this feature in our application. As it is a simple facet we will use aggregations of the Term Aggregations type, and they will be of the genre and certificate of the films.

Java Code

Let’s start coding our aggregation. We will create in our controller a new endpoint called /facets that will receive the search term.
In our ServiceCore we will create the method “getFacets” passing the parameters “term” and the list of aggregations, our list will be composed by “genre” and “certificate”.

Once the aggregation is created, just add it to our SearchRequest.

The next part is to extract the result list from the aggregation components, at this point we create a method that will extract the results and models in the output format that is a Map whose key is the name of the aggregate field and the value is a list that contains the groupings of genres and their quantity.

Running test

Let’s run a search for the term “batman”.

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

See that in the results we have 4 movies returned, and we have the genres:

  • Action: 4
  • Adventure: 2
  • Crime: 2
  • Animation: 1
  • Drama : 1

And certificates:

  • UA: 3
  • PG: 1
"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
]
}
]

Now let’s run the facets:

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

The results are exactly what we expected, this demonstrates that the aggregations are correct.

Facets results

In the next post we will add filters to the query to refine the results.

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.