Think of an index as a collection of records, a document as one JSON record, and a mapping as the rules for interpreting its fields.
Start with one product
{"name":"Blue shoes"
This document belongs to a products index. Its mapping can declare name as searchable text, price as a number and created_at as a date.
Suppose you need products priced below 200. A numeric price lets Elasticsearch compare numbers. A string representation serves different comparisons and is a poor substitute for numeric range queries.
The same input therefore needs a mapping chosen from the searches you want to support. JSON alone does not describe all indexing behavior.
Dynamic mapping automatically infers fields when data arrives. That is convenient for exploration, but an unexpected early value may establish an unsuitable type. Declare known production fields explicitly and control arbitrary new field names.
Adding a field is different from changing an existing field's type. The latter generally requires creating a new index and copying documents into it with reindexing.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate