How to Reduce Elasticsearch Storage by 35% (Two Mapping Mistakes to Avoid)
From the publisher’s feed, collected in the ZeroLabs newsroom.
1. IntroductionOur search cluster had grown to almost 600 GiB. Nobody planned for it. It just grew, the way most clusters do, one index at a time.When we finally audited it, the cluster wasn’t big because our data was big. It was big because we were storing our data badly. We fixed two things in our mappings, reindexed, and got back about 208 GiB. No data was lost. No features changed.Before: ~595 GiBAfter: ~387 GiBReclaimed: ~208 GiB, about 35%In this article we’ll cover the two mistakes: analyzing every string field as full text, and storing numeric IDs as strings. Both are common. There’s a good chance you’re making at least one of them right now.One note before we start. This is a storage story, and the 35% is measured. We didn’t benchmark CPU, so when I say a change reduces compute, I’m describing the mechanism, not a number we recorded. I’d rather be clear about that than dress up a guess.2. The SetupTo keep things concrete, I’ll use a made-up example that mirrors what we had: an online store with a few search indexes. The big one is orders, with millions of documents. Each order document has fields like status, currency, tracking_link, order_id, customer_id, and a nested customer.name.A few terms we’ll use:A mapping tells Elasticsearch the type of each field and how to index it.An analyzer breaks a text field into searchable tokens. It’s what makes full-text search work, and it isn’t free.A dynamic template is a rule that maps fields Elasticsearch hasn’t seen before. It’s convenient, and it’s also where most mapping mistakes hide.3. How Elasticsearch Stores a FieldBoth mistakes come down to one idea: the field type you choose decides what gets written to disk. So it’s worth a minute on what each type actually builds.A text field is run through an analyzer and stored in an inverted index, a map from each token to the documents that contain it. This is what powers full-text search. It is also the most expensive option, because every token you produce is another entry to store.A keyword field is not analyzed. The whole value is stored as a single term, and it's also kept in doc_values, a column-style structure used for sorting, filtering, and aggregations.A numeric type like long is stored in a BKD tree, a structure built specifically for numbers and ranges. It's compact, and it's fast to filter and sort on.Keep that hierarchy in mind: text is the heavy one, keyword is lighter, and a numeric type is the lightest for actual numbers. Both of our mistakes were choosing a heavier type than the field needed.4. Mistake #1: Analyzing Every String as Full TextThis was the big one. Most of the 208 GiB came from here.a) The ProblemWe had a dynamic template that matched every string field in the document:"dynamic_templates": [ { "all_strings_as_text": { "match_mapping_type": "string", "mapping": { "type": "text", "analyzer": "autocomplete", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }]This rule said: take any string field and index it two ways at once. First as text with an autocomplete analyzer, then again as a keyword sub-field.The text half is where it hurts. An autocomplete analyzer is an edge-ngram tokenizer. It indexes every growing prefix of a value so type-ahead search works. Take the value "electronics". It produces roughly 10 tokens, one per prefix:e, el, ele, elec, elect, electr, electro, electron, electroni, electronicsSo a single value becomes roughly 10 entries in the inverted index instead of 1. (Rough illustrative figures, the point is the multiplier, not the exact count.) That’s a great trade when a human types into a search box. It’s pure waste for status, currency, or tracking_link, fields we only ever filter on or display. We were building autocomplete for fields nobody autocompletes, and paying for a keyword copy on top.b) The FixApply the expensive analyzer only to the fields you actually search as text, mostly names. Let everything else map as a plain keyword. With dynamic templates you can split it by path:"dynamic_templates": [ { "names_as_text": { "path_match": "*.name", "mapping": { "type": "text", "analyzer": "autocomplete", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, { "strings_as_keyword": { "match_mapping_type": "string", "mapping": { "type": "keyword", "ignore_above": 256 } } }]The rule is simple. An analyzer is a cost you pay for a search feature. Don’t pay it for features you don’t use. Most string fields are filtered, sorted, or displayed, not full-text searched. Those want keyword and nothing more.c) Why It Also Helps CPUEvery token you don’t create is analysis work you skip at write time, and a posting list you don’t merge later. Dropping autocomplete analysis on fields that never needed it removes a steady tax on every single write.5. Mistake #2: Storing IDs as StringsOur documents were full of identifiers, order_id, customer_id, product_id. Every one was mapped as keyword.a) The Problem{ "order_id": { "type": "keyword" }, "customer_id": { "type": "keyword" } }keyword works for IDs, but it's the wrong tool for a numeric one. A keyword value goes into a string term dictionary, and every unique value adds an entry. For a high-cardinality field like a primary key, nearly every value is unique, so the dictionary grows almost as large as the dataset itself.Compare the two on a single ID like 10042837:As a keyword, it's stored as text, roughly 8 bytes for the characters, plus its own slot in the term dictionary and doc-values.As a long, it's packed into a BKD tree as an 8-byte integer with no term dictionary at all.(Again, illustrative, but the shape is real.) Per value the difference looks small. Multiply it by millions of unique IDs across several ID fields per document, and the term-dictionary overhead alone becomes a serious chunk of the index.b) The FixMap numbers as numbers.{ "order_id": { "type": "long" }, "customer_id": { "type": "long" } }The rule of thumb: if a field is a number you filter or range on, and you never need prefix or substring matching, map it as a numeric type. Keep keyword for real strings, codes, slugs, enums.c) Why It Also Helps CPUA term or range query on a BKD tree (customer_id: 42, order_id > 1000) runs straight against the tree and skips the string term-dictionary lookup. Cheaper queries, cheaper indexing, smaller field data.6. When These Are Actually FineNeither fix is absolute. There are real cases where the “heavy” choice is correct:Keep text analysis when users genuinely full-text search a field, names, titles, descriptions, anything with type-ahead.Keep keyword on a number when the value is really an identifier you treat as a string, like a zip code with leading zeros, or a numeric code you prefix-match on.The mistake isn’t using text or keyword. It's applying them by default, to every field, without asking whether that field needs them.7. How to Audit Your Own ClusterYou don’t need a project for this. You need ten minutes and the _cat APIs.a) Find your biggest indexes. Start at the top, that’s where the savings are.GET _cat/indices?v&s=store.size:descb) Read your mappings and question every field.GET your-index/_mappingFor each text field, ask: is this actually full-text searched, or am I paying for an analyzer I don't use? For each keyword holding a number, ask: do I ever prefix-match this, or is it really a long?c) See where the bytes go. This breaks storage down per field, and usually points straight at one over-analyzed field.POST your-index/_disk_usage?run_expensive_tasks=true8. The ResultsAcross the whole cluster, the two changes reclaimed about 208 GiB, roughly 35%, with no data loss and no feature changes.Before: ~595 GiBAfter: ~387 GiBReclaimed: ~208 GiB, about 35%The savings weren’t spread evenly. A handful of our largest indexes drove most of it, the ones with deeply nested documents full of IDs and rarely-searched strings. That’s the usual pattern: a few big indexes carry most of the waste, so fixing the top of your _cat/indices list gets you most of the win.9. Final ThoughtsNone of this was clever. We didn’t tune a single exotic setting. We just stopped letting defaults decide the field type for us.An analyzer is a cost you pay for a search feature. Don’t apply one to fields you don’t search as text. A catch-all “every string is text” dynamic template is almost always a mistake.Map numbers as numbers. keyword on a high-cardinality numeric ID leaves both storage and query speed on the table.A cluster ends up 35% too big rarely because of one big blunder. It’s a default, copied into a template, inherited by dozens of indexes, and never questioned. The fix isn’t sophistication. It’s going back and asking the question of each field. Ten minutes with _cat/indices and _disk_usage will probably find storage hiding in your cluster right now.How to Reduce Elasticsearch Storage by 35% (Two Mapping Mistakes to Avoid) was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.