0
0
Elasticsearchquery~3 mins

Why Numeric field types in Elasticsearch? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your price searches could be instant and always correct, without extra work?

The Scenario

Imagine you have a huge list of product prices stored as plain text in your search database. You want to find all products cheaper than $50, but since prices are text, you have to manually convert and compare each one every time.

The Problem

This manual way is slow because every search needs extra steps to convert text to numbers. It's also error-prone since text sorting treats '100' as less than '50'. This causes wrong search results and wastes time.

The Solution

Using numeric field types in Elasticsearch means prices are stored as real numbers. Elasticsearch can quickly compare, sort, and filter these values without extra work. This makes searches fast, accurate, and easy.

Before vs After
Before
GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "lt": "50"
      }
    }
  }
}
After
PUT /products
{
  "mappings": {
    "properties": {
      "price": { "type": "float" }
    }
  }
}

GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "lt": 50
      }
    }
  }
}
What It Enables

It enables lightning-fast and precise numeric searches, filters, and sorting on your data.

Real Life Example

Online stores use numeric field types to quickly find products under a certain price or within a price range, giving shoppers instant results.

Key Takeaways

Storing numbers as text causes slow and wrong searches.

Numeric field types store real numbers for fast, accurate queries.

This improves search speed and result quality in Elasticsearch.