Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between text and keyword fields in Elasticsearch mappings, and why do teams often map a string field as both?

A text field is analysed into individual terms for full-text search, while a keyword field is stored as one single term for exact matching, sorting and aggregations. Teams often map a string as both, using a multi-field, so they can search the text and sort or aggregate on the keyword; dynamic mapping does exactly this by default, creating a text field with a .keyword subfield that has ignore_above 256. Aggregating on a text field would need fielddata, which is memory heavy and disabled by default.

Why this question is asked

Choosing between text and keyword is the most frequent mapping decision in Elasticsearch and OpenSearch, and a wrong choice shows up as searches that miss obvious matches, sorting that fails with an error, or aggregations that eat the heap. Interviewers ask this to make sure we understand what each type stores and which operations each supports. In this answer, we will see what each field type does to a value, look at the default dynamic mapping, use a multi-field, and cover ignore_above and fielddata.

1What Each Type Does to a Value

A text field runs the value through an analyzer. The standard analyzer splits 'Apple iPhone 16 Pro' into the terms apple, iphone, 16 and pro, and those terms go into the inverted index. A match query for 'iphone pro' is analysed the same way and finds the document. That is full-text search: matching by words, in any case and order, with relevance scoring.

A keyword field skips analysis. The whole value, exactly as written, becomes one term: 'Apple iPhone 16 Pro'. It matches only a term query for exactly that string, which is right for ids, status values, tags, emails and brand names used as filters. Keyword fields also get doc values by default, a column-oriented copy of the values on disk, which is what sorting and aggregations read.

text compared with keyword
Questiontextkeyword
Stored in the index asanalysed terms: apple, iphone, 16, proone term: Apple iPhone 16 Pro
Full-text match on 'iphone pro'yes, scoredno
Exact term 'Apple iPhone 16 Pro'no, the term does not existyes
Sort and aggregateonly with fielddata, off by defaultyes, through doc values
Typical usetitles, descriptions, messagesids, codes, status, tags, filters

2What Dynamic Mapping Creates

When we index a document with a new string field and no explicit mapping, Elasticsearch guesses the mapping. For strings it cannot know whether we will search or aggregate, so it creates both.

Java
PUT /products/_doc/1
{ "brand": "Apple" }
GET /products/_mapping
// "brand": {
// "type": "text",
// "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
// }

So the field brand is text, and brand.keyword is a keyword subfield of the same value. This is convenient for exploration, but it indexes every string twice, which costs disk and indexing time, so production indices usually define explicit mappings and keep both forms only where both are needed.

3Using a Multi-Field Deliberately

A multi-field is one source value indexed in more than one way. We search one form and sort or aggregate on the other. A product title is the classic case: users search it by words, but a listing page may sort by it alphabetically.

Java
PUT /products_v2
{ "mappings": { "properties": {
"title": { "type": "text",
"fields": { "sort": { "type": "keyword", "ignore_above": 256 } } },
"brand": { "type": "keyword" },
"description": { "type": "text" } } } }
GET /products_v2/_search
{ "query": { "match": { "title": "iphone pro" } },
"sort": [ { "title.sort": "asc" } ],
"aggs": { "brands": { "terms": { "field": "brand" } } } }
// match on text, sort on the keyword subfield, aggregate on a keyword field

Notice that brand is keyword only, because nobody needs to search brand names word by word here, and description is text only, because nobody sorts by it. A multi-field is a tool, not a default.

4Ignore_above and Long Values

ignore_above tells a keyword field not to index values longer than the given number of characters. Dynamic mapping sets it to 256. A longer value is still kept in _source and returned with the document, but it gets no term in the keyword field, so a term query, sort or aggregation on it behaves as if the value were missing.

The reason is cost and limits. A long description as a single keyword term is useless for exact matching and expensive to store as a term and in doc values, and Lucene has a hard limit of 32,766 bytes for a single term. If a field such as a URL can legitimately exceed 256 characters and must be filterable, we raise ignore_above for that field explicitly.

5Why Aggregating on text Fails

Aggregations need, for each document, the field's values, which is the opposite direction of the inverted index. Keyword fields have that in doc values on disk. Text fields do not support doc values, so the only way is fielddata, an in-memory structure built on the heap by un-inverting the index the first time it is needed.

Java
GET /products/_search
{ "aggs": { "titles": { "terms": { "field": "title" } } } }
// error: Text fields are not optimised for operations that require per-document field
// data like aggregations and sorting ... use a keyword field instead
// (exact wording depends on the version)

We can enable fielddata on a text field, but it can use a lot of heap and it aggregates individual analysed terms like 'iphone' and 'pro', which is rarely what we want. The right fix is almost always a keyword field or a keyword subfield.

6Follow-up questions

When would we choose text only, without a keyword?

For long prose such as descriptions, comments or log messages, which are searched but never sorted, aggregated or matched exactly. Adding a keyword there would waste space and usually exceed ignore_above anyway.

Can we add a keyword subfield to an existing text field?

Yes, adding a new subfield to the mapping is allowed. Existing documents do not get values in it until they are reindexed, for example with update_by_query. Changing the type of an existing field, by contrast, requires a new index.

How do we make keyword matching case-insensitive?

Add a normalizer with a lowercase filter to the keyword field. It is applied at index time and to term-level queries, so 'apple' matches 'Apple'.

7Conclusion

In this answer, we saw that text fields are analysed into terms for scored full-text search, while keyword fields store the exact value as one term for exact matches, sorting and aggregations through doc values. Dynamic mapping creates text with a .keyword subfield and ignore_above 256, a multi-field lets us search one form and sort or aggregate the other, values longer than ignore_above are not indexed in the keyword, and aggregating on text needs fielddata, which is heap heavy and disabled by default.

What to say in the interviewAbout 90 seconds aloud

Open with

The difference is what gets indexed. A text field is analysed, so 'Apple iPhone 16 Pro' becomes terms like apple, iphone, 16 and pro, which is what full-text search with match queries and relevance scoring needs.

Then, if they want more

A keyword field is not analysed; the whole value is a single term, so it is for exact matches with term queries, and it has doc values by default, which is what sorting and aggregations use.

Teams map strings as both through multi-fields because they often need both: search the title by words, but sort by it, or search a brand and also build a facet on it.

Dynamic mapping actually does this by default for any new string, creating a text field with a keyword subfield called .keyword, and that subfield has ignore_above 256, meaning longer values are still in _source but not indexed in the keyword, so they will not show up in term queries or aggregations.

In a production mapping I would choose explicitly: keyword only for ids, codes, status and tags, text only for long descriptions, and a multi-field only where both are needed, since indexing twice costs space.

If someone tries to aggregate on a text field, it fails because text has no doc values, and the alternative, fielddata, is built on the heap, is memory heavy and disabled by default, so the fix is a keyword field.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Elasticsearch documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between text and keyword fields in Elasticsearch mappings, and...": https://scrivio-interview-library.netlify.app/databases/search-engines/287b9856b4/