Home > Software design >  How to swap two elsticsearch indexes
How to swap two elsticsearch indexes

Time:12-02

I want to implement cash for highly loaded elasticsearch-based search system. I want to store cash in special elastic index. The problem is in cache warm-up: once an hour my system needs to update cached results with the fresh ones.

So, I'm creating a new empty index and fill it with updated results, then I need to swap old index and new index, so users can use fresh cached results.

The question is: how to swap two elasticsearch indexes efficiently?

CodePudding user response:

For this kind of scenario you use something that is called "index alias swapping".

You have an alias that points to your current index, you fill a new index with the fresh records, and then you point this alias to the new index.

Something like this:

  1. Current index name is items-2022-11-26-001
  2. Create alias items pointing to items-2022-11-26-001
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "items-2022-11-26-001",
        "alias": "items"
      }
    }
  ]
}
  1. Create new index with fresh data items-2022-11-26-002
  2. When it finishes, now point the items alias to items-2022-11-26-002
POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "items-2022-11-26-001",
        "alias": "items"
      }
    },
    {
      "add": {
        "index": "items-2022-11-26-002",
        "alias": "items"
      }
    }
  ]
}
  1. Delete items-2022-11-26-001

You run all your queries against "items" alias that will act as an index.

References:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

  • Related