Agent Wranglers

SRH-9Recommended

Rebuild an index under a new name and switch an alias

Point the app at an alias, never at an index by name. Build each new version of the index beside the old one, compare them, then move the alias in one step.

How to do it

Changing a mapping, an analyser or the embedding model means building the index again. Rebuilding in place leaves search empty or half full for the duration. Build products_v8 while products_v7 serves, and replay the updates that arrived meanwhile from the queue. Run the fixed query set against the new index, and switch only when it passes. Elasticsearch and OpenSearch move an alias in one atomic call. In PostgreSQL, build the new column and index, then swap them in one transaction. Keep the old index for a day, so a switch back is one call.

Examples

HTTP example

POST /_aliases
Content-Type: application/json

{ "actions": [
  { "remove": { "index": "products_v7", "alias": "products" } },
  { "add": { "index": "products_v8", "alias": "products" } }
] }

Python example

def switch_alias(client, alias: str, old_index: str, new_index: str) -> None:
    client.indices.update_aliases(body={"actions": [
        {"remove": {"index": old_index, "alias": alias}},
        {"add": {"index": new_index, "alias": alias}},
    ]})

JavaScript example

export async function switchAlias(client, alias, oldIndex, newIndex) {
  await client.indices.updateAliases({ body: { actions: [
    { remove: { index: oldIndex, alias } },
    { add: { index: newIndex, alias } },
  ] } });
}

How to check

During a full rebuild on staging, a script searches every second. No search fails or returns an empty page. The app's settings name the alias, and a code search finds no versioned index name outside the rebuild job.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/search/index-contents-and-sync/reindex-with-alias">Agent Wranglers search playbook</a>. In code you write, add a comment next to it with the same link.