When a student age is indexed as text, Elasticsearch blocks sorting by default. Fielddata enables text-field values to support sorting and aggregations. section 4.2.2 section 13.2.2
Set "fielddata": true in the field mapping to load per-document text values. Elasticsearch stores this data in the cluster’s on-heap fielddata cache, so enabling it can consume substantial memory and reduce performance. section 4.2.2 section 13.2.2
Enable fielddata on a text field
PUT students_with_fielddata_setting { "mappings": { "properties": { "age": { "type": "text", "fielddata": true } } } } This mapping permits operations such as sorting on age, but age.keyword is generally preferred when Elasticsearch creates that keyword multi-field. Sorting on age.keyword avoids enabling fielddata for the text field. section 4.2.2
Choose fielddata deliberately:
- Prefer keyword fields for sorting and aggregations when a keyword multi-field is available. section 4.2.2 section 13.2.2
- Use fielddata only when text-field operations are specifically required and their memory cost is acceptable. section 4.2.2 section 13.2.2
