Menu

Filtering, Sorting, and Pagination in Elasticsearch

Filtering, Sorting, and Pagination in Elasticsearch

In the previous lesson, you learned how Elasticsearch searches data and returns the most relevant results.

However, simply finding matching documents isn't always enough.

Imagine searching for:

Smartphones

An online shopping website may return thousands of matching products.

Would you want to scroll through thousands of results to find the right phone?

Probably not.

Instead, you would use filters such as:

  • Brand: Samsung
  • Price: Under ₹30,000
  • Rating: 4★ and above
  • Storage: 256 GB

You might also sort the products by:

  • Lowest Price
  • Highest Rating
  • Newest First

Finally, since thousands of products exist, the website doesn't display them all at once. Instead, it shows a small number of products on each page.

These three features are called:

  • Filtering
  • Sorting
  • Pagination

Together, they help users quickly find exactly what they're looking for.

Understanding Filtering

filter removes documents that don't meet specific conditions.

Think of filtering like using a sieve.

Imagine you have a basket filled with fruits.

Basket

🍎 Apple

🍌 Banana

🍊 Orange

🍇 Grapes

🍎 Apple

🥭 Mango

Suppose you only want apples.

The sieve removes everything else.

Result:

🍎 Apple

🍎 Apple

Filtering in Elasticsearch works the same way.

It removes documents that don't satisfy your conditions.

Real-Life Example

Imagine shopping for laptops.

You search:

Laptop

The website displays 15,000 products.

Now you apply filters.

Brand:

  • Dell

Price:

  • Under ₹70,000

RAM:

  • 16 GB

Operating System:

  • Windows 11

Instead of showing all 15,000 laptops, Elasticsearch returns only those that satisfy every selected condition.

Common Types of Filters

Elasticsearch supports many kinds of filters.

Let's look at the most common ones.

1. Exact Value Filter

Suppose every product has a brand.

Examples:

  • Dell
  • HP
  • Lenovo
  • Asus

If you choose:

Brand = Dell

Elasticsearch returns only Dell products.

2. Numeric Filter

Numeric filters work with numbers.

Examples:

  • Price
  • Rating
  • Age
  • Quantity

Imagine searching for:

Smartphones under ₹25,000

Elasticsearch removes all products priced above ₹25,000.

3. Date Filter

Many applications store dates.

Examples include:

  • Order Date
  • Registration Date
  • Delivery Date
  • Publication Date

Suppose an online news website allows users to view:

Articles published this week.

Elasticsearch filters older articles and displays only recent ones.

4. Boolean Filter

Boolean fields contain only two values:

  • true
  • false

Example:

Only show products currently available.

Field:

in_stock = true

Products that are out of stock are automatically excluded.

Multiple Filters

Users usually apply several filters together.

Imagine searching for shoes.

Search:

Running Shoes

Filters:

  • Brand = Nike
  • Size = 9
  • Price < ₹5,000
  • Rating > 4.5

Elasticsearch returns only products matching all these conditions.

This significantly reduces the number of results.

How Filtering Works

Suppose Elasticsearch initially finds:

20,000 Products

Brand Filter

6,000 Products

Price Filter

1,800 Products

Rating Filter

450 Products

Availability Filter

320 Products

Instead of examining thousands of products, the customer now sees only the most relevant options.

Understanding Sorting

Filtering reduces results.

Sorting decides the order in which those results appear.

Imagine you have these laptop prices:

₹45,000

₹60,000

₹38,000

₹80,000

Without sorting, the order is random.

After sorting from lowest to highest:

₹38,000

₹45,000

₹60,000

₹80,000

The same principle applies to Elasticsearch.

Common Sorting Options

Most applications let users sort by:

Price

  • Low to High
  • High to Low

Rating

Highest-rated products appear first.

Newest

Recently added products appear first.

Popularity

Products with the highest sales or views appear first.

Alphabetical Order

Products sorted from:

A → Z

or

Z → A

Real-Life Example

Imagine searching for:

Bluetooth Speaker

After filtering, 120 products remain.

You choose:

Sort by Price (Lowest First)

Elasticsearch rearranges the products without changing the filtered results.

Understanding Pagination

Imagine Elasticsearch returns 50,000 products.

Should the website display all of them on one page?

No.

That would:

  • Slow down the website.
  • Consume more memory.
  • Make browsing difficult.

Instead, results are divided into smaller pages.

This process is called Pagination.

Real-Life Analogy

Imagine reading a book with 500 pages.

The publisher doesn't print every page on one giant sheet of paper.

Instead, the content is divided into individual pages.

Pagination in Elasticsearch works the same way.

Example

Suppose a search returns:

100 products.

The website displays:

20 products per page.

The pages look like this:

Page 1

Products 1–20

Page 2

Products 21–40

Page 3

Products 41–60

and so on.

This improves performance and makes browsing much easier.

Why Pagination is Important

Imagine loading:

100,000 products

at once.

Problems include:

  • Slow loading time.
  • Higher memory usage.
  • Poor user experience.
  • Increased network traffic.

Pagination solves these issues by loading only a small portion of results at a time.

Combining Search, Filter, Sort, and Pagination

Let's see how a complete search works.

A customer visits an online electronics store.

Step 1

Search:

Gaming Laptop

Elasticsearch finds:

3,500 products.

Step 2

Apply Filters:

  • Dell
  • RTX Graphics
  • Under ₹1,20,000

Results become:

180 products.

Step 3

Sort:

Highest Rating First.

Products are reordered.

Step 4

Pagination:

Display:

20 products per page.

The customer now browses page by page instead of loading all 180 products simultaneously.

Real-World Applications

These features are used almost everywhere.

Amazon

  • Filter by price
  • Filter by brand
  • Sort by popularity
  • Pagination

Netflix

  • Filter by genre
  • Filter by language
  • Sort by release year
  • Browse multiple pages

Flipkart

  • Customer ratings
  • Offers
  • Delivery options
  • Price sorting

LinkedIn

Search:

Software Engineer

Filters:

  • Location
  • Experience
  • Company
  • Job Type

Sort:

Most Recent

Google Search

Google also uses similar concepts.

You can filter by:

  • Images
  • Videos
  • News
  • Date

and browse through multiple pages of search results.

Why Filtering is Faster Than Searching

One interesting feature of Elasticsearch is that filters are highly optimized.

Unlike search queries, filters don't calculate relevance scores.

They simply check whether a document satisfies a condition.

Because of this, filtering is generally faster and more efficient for exact conditions like price ranges, categories, dates, or availability.