AI Hints
Home›Elasticsearch Tutorial›Nested, Reindex and Aliases
Elasticsearch · Lesson 47 of 120

Changing a Mapping: Reindex

You can’t edit, you copy.

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

You realise brand should have been a keyword. But you can't change the type of an existing field, because the data is already indexed that way. The fix is to create a new index, and copy the data across.

  • Wrong type
  • Can't edit it
  • Copy to a new index

Reindex into a new index

First, create products v2, with brand as a keyword. Then reindex copies every document from products into products v2. Run it. All forty nine products are copied, and brand is now a keyword.

Dev Tools Consoletext
PUT products_v2
{ "mappings": { "properties": { "brand": { "type": "keyword" } } } }

POST _reindex?refresh=true&filter_path=total,created
{
  "source": { "index": "products" },
  "dest":   { "index": "products_v2" }
}

GET products_v2/_mapping/field/brand?filter_path=*.mappings.*.mapping
Output
# PUT products_v2 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products_v2"
}

# POST _reindex 200 OK
{"total": 49, "created": 49}

# GET products_v2/_mapping/field/brand 200 OK
{
  "products_v2": {
    "mappings": {
      "brand": {"mapping": {"brand": {"type": "keyword"}}}
    }
  }
}
Also uses _load
products

Mapping rules

You can add new fields to a mapping at any time. You can't change the type of an existing field. To change one, reindex into a new index. And to avoid downtime, point your app at an alias.

keyvalue
Add a fieldyes, any time
Change a typeno
Fix_reindex to a new index
No downtimeuse an alias

Summary

You can add fields, but not change their types. Create a new index, and reindex the data into it. Next, aliases, so your app never notices.

Key takeaways
  • Can't change types
  • New index
  • _reindex
Next up: Aliases — a name that can move