0
0
Elasticsearchquery~5 mins

Boosting query in Elasticsearch

Choose your learning style9 modes available
Introduction

A boosting query helps you show some search results as more important and others as less important.

You want to show favorite products higher in search results.
You want to lower the rank of outdated or less relevant items.
You want to promote certain keywords but still show other matches.
You want to reduce the score of results that contain unwanted terms.
Syntax
Elasticsearch
{
  "query": {
    "boosting": {
      "positive": { <query> },
      "negative": { <query> },
      "negative_boost": <number>
    }
  }
}

positive is the query for results you want to boost.

negative is the query for results you want to reduce in score.

Examples
This boosts results with "apple" in the title and lowers those with "banana".
Elasticsearch
{
  "query": {
    "boosting": {
      "positive": { "match": { "title": "apple" } },
      "negative": { "match": { "title": "banana" } },
      "negative_boost": 0.5
    }
  }
}
This promotes electronics category but lowers outdated items.
Elasticsearch
{
  "query": {
    "boosting": {
      "positive": { "term": { "category": "electronics" } },
      "negative": { "term": { "category": "outdated" } },
      "negative_boost": 0.3
    }
  }
}
Sample Program

This query boosts documents mentioning "fresh fruit" and lowers those mentioning "rotten" in the description.

Elasticsearch
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "description": "fresh fruit"
        }
      },
      "negative": {
        "match": {
          "description": "rotten"
        }
      },
      "negative_boost": 0.2
    }
  }
}
OutputSuccess
Important Notes

The negative_boost value must be between 0 and 1. Smaller means stronger lowering.

Boosting query helps balance showing good matches while pushing down less relevant ones.

Summary

Boosting query lets you raise some results and lower others in search results.

Use positive and negative queries with a negative_boost number.

This helps make search results more useful and relevant for users.