Trendyol Search Filters: How to Aggregate with Elasticsearch?

Mert Bahardoğan11 Jul 2025

Problem

A marketplace search page shows filters such as brand, size, category and free shipping, each with a product count that must follow the query and the selected filters. When a brand is selected, the brand filter still has to show all brands. Aggregating a whole index of millions of documents also causes timeouts and CPU load.

Example from the article: on Trendyol's search page a "nike shoes" request must return the shoe products, the counts for category and free shipping inside the selected brand, and the counts for every brand inside the category.

Solution in brief

Narrow the data with a main query first, then compute all filter counts with Elasticsearch terms aggregations in one request. The category filter goes into the main query. The brand filter goes into post_filter, so it changes the hits but not the aggregations. The aggregations are split in two groups: one group with the brand filter applied (other facets), one without it (the brand facet itself).

Questions this case answers
  • How to compute facet counts for several filters at once, where each facet must ignore its own selected value but respect all the other filters, in a single query over a large sharded index
  • How to build search page filters (brand, category, free shipping) whose counts stay consistent with the current selection in Elasticsearch?
  • How to keep the full list of brands visible after the user picks one brand?
  • How to keep filter aggregations fast and avoid timeouts on an index with millions of products?

Tools and methods

  • Elasticsearch terms aggregation — one bucket per unique field value, used for filter counts.
  • Main query (bool filter) — restricts the documents before aggregation.
  • post_filter — filters the hits after aggregations are computed.
  • Filter aggregation — applies a selected filter to a group of sub-aggregations.
  • eager_global_ordinals — speeds up aggregation queries.
Continue reading — it's free

Google or a one-time email code. No password.