Numeric field types let you store numbers in Elasticsearch so you can search, sort, and calculate with them easily.
Numeric field types in 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.
PUT /products
{
"mappings": {
"properties": {
"price": { "type": "float" },
"stock": { "type": "integer" }
}
}
}PUT /users
{
"mappings": {
"properties": {
"age": { "type": "short" },
"score": { "type": "scaled_float", "scaling_factor": 100 }
}
}
}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.
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
}
}
}
}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.
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.
Practice
Which numeric field type in Elasticsearch should you use to store whole numbers without decimals?
Solution
Step 1: Understand numeric field types
Integer type stores whole numbers without decimals, suitable for counts or IDs.Step 2: Compare with other types
Float and double store decimal numbers; text is for strings, not numbers.Final Answer:
integer -> Option BQuick Check:
Whole numbers = integer [OK]
- Choosing float or double for whole numbers
- Using text type for numbers
- Confusing integer with long for small numbers
Which of the following is the correct way to define a long numeric field in an Elasticsearch mapping?
{
"mappings": {
"properties": {
"user_id": {
"type": "_____"
}
}
}
}Solution
Step 1: Identify the field type for large whole numbers
Long type stores larger whole numbers than integer, suitable for big IDs.Step 2: Match the correct type string in mapping
Use "long" as the type value in the mapping JSON for such fields.Final Answer:
"long" -> Option AQuick Check:
Big whole numbers = "long" [OK]
- Using "integer" for very large numbers
- Using "keyword" which is for text
- Using "float" for whole numbers
Given this Elasticsearch mapping snippet, what type of number can price store?
{
"mappings": {
"properties": {
"price": {
"type": "float"
}
}
}
}Solution
Step 1: Check the type assigned to price
The type is "float", which stores decimal numbers with floating point precision.Step 2: Understand what float means
Float allows decimals, unlike integer or long which store whole numbers only.Final Answer:
Decimal numbers with floating point -> Option DQuick Check:
Float type = decimals allowed [OK]
- Thinking float stores only whole numbers
- Confusing float with text or boolean
- Assuming float stores exact decimal precision always
What is wrong with this Elasticsearch mapping for a numeric field?
{
"mappings": {
"properties": {
"score": {
"type": "integer",
"index": "not_analyzed"
}
}
}
}Solution
Step 1: Review the 'index' parameter usage
Numeric fields do not support 'index' with 'not_analyzed'; this is for text fields.Step 2: Confirm correct mapping for numeric fields
Numeric fields are indexed by default and do not use 'not_analyzed'.Final Answer:
'index' parameter is invalid for numeric fields -> Option CQuick Check:
Numeric fields don't use 'index': 'not_analyzed' [OK]
- Using text field options on numeric fields
- Changing type unnecessarily
- Ignoring Elasticsearch mapping syntax rules
You want to store product weights that can be very large and may have decimals. Which Elasticsearch numeric field type is best?
Solution
Step 1: Analyze the requirements for product weights
Weights can be very large and have decimals, so the type must support large range and decimals.Step 2: Compare float and double types
Float supports decimals but has less precision and smaller range than double.Step 3: Choose the best type for large decimal numbers
Double supports large decimal numbers with higher precision, suitable for weights.Final Answer:
double -> Option AQuick Check:
Large decimals = double type [OK]
- Choosing integer or long which don't support decimals
- Using float which may lose precision
- Ignoring size and precision needs
Ready to prove it?
Pass the test and this pattern moves unknown
