0
0
Elasticsearchquery~3 mins

Why Function score query in Elasticsearch? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could make search results smarter by mixing relevance with your own rules in one simple step?

The Scenario

Imagine you have a huge list of products and you want to find the best ones based on both how well they match a search word and how popular they are. Doing this by hand means searching, then sorting by popularity separately, and trying to combine results yourself.

The Problem

This manual way is slow and tricky. You might miss some good products because you can't easily mix search quality and popularity scores. It's like trying to pick the best apples by looking at color first, then weight, but not both together smoothly.

The Solution

The function score query lets you mix the search match score with other factors like popularity in one step. It automatically adjusts scores so the best results consider both what you searched for and extra qualities you care about.

Before vs After
Before
{ "query": { "match": { "name": "phone" } } }
// Then sort results by popularity separately
After
{ "query": { "function_score": {
  "query": { "match": { "name": "phone" } },
  "field_value_factor": { "field": "popularity" }
} } }
What It Enables

You can easily find results that balance relevance and custom factors, making searches smarter and more useful.

Real Life Example

In an online store, customers search for "phone" and get results that not only match the word but also show popular and highly rated phones first.

Key Takeaways

Manual searching and sorting is slow and incomplete.

Function score query combines search relevance with custom scoring.

This makes search results smarter and more tailored.