A schema is a formal description of a data model: its fields, their data types, and the rules used to handle them. Structured data follows a predefined shape—for example, dates, numbers, and Boolean values must use expected formats. In Elasticsearch, the schema is called a mapping and describes the JSON fields in documents stored by an index. section 1.2.1 section 4.1
Mapping gives Elasticsearch the clues it needs to index each field correctly. A text field can use full-text rules, while exact-value fields such as dates and numbers support operations including sorting, filtering, and aggregations. Accurate field types help produce accurate search results. section 4.0 section 4.1.1
Choose how the schema is created:
- Use explicit mapping to declare fields and types before indexing. This is the preferred approach when the data model is known. section 4.0 section 4.8
- Use dynamic mapping to let Elasticsearch infer field types when it first encounters fields. This is convenient during development, but offers less control. section 4.8
- Treat the mapping as one definition for an index, with every field processed according to its declared rule. section 4.1
A car schema
Create an index with a mapping when car data needs searchable make and model fields plus a date-aware registration year. The properties object holds field declarations; make and model use text, while registration_year uses date with an explicit dd-MM-yyyy format. Declaring that format ensures the value is indexed as a date rather than treated as undifferentiated text. section 6.2.4 section 4.1
Listing 6.10 Creating an index with field mappings for a car document
PUT cars_with_mappings
{
"mappings": { #1
"properties": { #2
"make":{
"type": "text" #3
},
"model":{
"type": "text"
},
"registration_year":{ #4
"type": "date",
"format": "dd-MM-yyyy" #5
}
}
}
}
Inspect the mapping request to see the index-level mappings object, its properties container, and field-level type and format declarations together. section 6.2.4
Plan schema changes before an index becomes live. Elasticsearch does not allow an existing indexed field to change type, because data was stored under the original definition. To correct a type, create a new index with the updated schema, reindex the old data, validate the new index, and switch the application over; aliases can support a seamless switch. section 4.3.3 section 4.8
Product-search schema choices
An electrical-products index can declare price and user_ratings as double while defining other attributes as text. Fields may use multiple types such as text and keyword when both full-text searching and term-level queries are needed for values such as color or energy rating. section 11.1.1











































