AI Hints
Elasticsearch · Lesson 41 of 120

Explicit Mappings

Choose the types.

⏱ 2 min read▶ Video at 37:44🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Guessing goes wrong. A zip code like zero two one three nine becomes a number, and loses its leading zero. A product code gets split into words. In real projects, you write the mapping yourself.

  • Guesses go wrong
  • Zip codes, codes
  • Write it yourself

A mapping by hand

We create the index with a mappings section. Name is text, for full text search. Brand and category are keywords, for exact filters. Price is a scaled float, stored in cents. Run it. The index is created with our types.

Dev Tools Consoletext
PUT products
{
  "mappings": {
    "properties": {
      "name":     { "type": "text" },
      "brand":    { "type": "keyword" },
      "category": { "type": "keyword" },
      "price":    { "type": "scaled_float", "scaling_factor": 100 },
      "in_stock": { "type": "boolean" },
      "added":    { "type": "date" }
    }
  }
}
Output
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

Common field types

Text is for words you search. Keyword is for exact values, sorting and aggregations. There are number types like integer, long, float and scaled float. And date, boolean, geo point, and dense vector for AI.

keyvalue
textfull-text search
keywordexact, sort, aggregate
integer / long / floatnumbers
date, booleandates, true/false
geo_point, dense_vectormaps, vectors

Summary

An explicit mapping lets you choose each field's type, instead of trusting a guess. Next, the most important choice, text or keyword.

Key takeaways
  • mappings
  • properties
  • type
Next up: text vs keyword — the big choice