Numeric field types let you store numbers in Elasticsearch so you can search, sort, and calculate with them easily.
0
0
Numeric field types in Elasticsearch
Introduction
When you want to store ages, prices, or quantities in your data.
When you need to filter results by number ranges, like products costing between $10 and $50.
When you want to sort search results by numeric values, like highest rating first.
When you want to do math operations on your data, like averages or sums.
When you want to store dates as timestamps (numbers) for fast searching.
Syntax
Elasticsearch
PUT /my_index
{
"mappings": {
"properties": {
"field_name": {
"type": "integer" | "long" | "float" | "double" | "short" | "byte" | "half_float" | "scaled_float"
}
}
}
}Choose the numeric type based on the size and precision you need.
Scaled_float lets you store decimals as integers with a scaling factor for accuracy.
Examples
This example creates a products index with price as a decimal number and stock as a whole number.
Elasticsearch
PUT /products
{
"mappings": {
"properties": {
"price": { "type": "float" },
"stock": { "type": "integer" }
}
}
}Here, age is a small integer and score is a decimal stored as an integer multiplied by 100.
Elasticsearch
PUT /users
{
"mappings": {
"properties": {
"age": { "type": "short" },
"score": { "type": "scaled_float", "scaling_factor": 100 }
}
}
}Sample Program
This creates a library index with pages as an integer and rating as a float. Then it adds a book and searches for books with at least 300 pages.
Elasticsearch
PUT /library
{
"mappings": {
"properties": {
"pages": { "type": "integer" },
"rating": { "type": "float" }
}
}
}
POST /library/_doc
{
"pages": 350,
"rating": 4.5
}
GET /library/_search
{
"query": {
"range": {
"pages": {
"gte": 300
}
}
}
}OutputSuccess
Important Notes
Always pick the smallest numeric type that fits your data to save space.
Elasticsearch stores numbers in a way that makes searching and sorting very fast.
Remember to define your mappings before adding data to avoid errors.
Summary
Numeric field types store numbers for searching, sorting, and calculations.
Choose types like integer, float, or scaled_float based on your data needs.
Define numeric fields in mappings before adding documents.