Skip to content

Re-index ElasticSearch Index

Sometimes an index gets broken and here is how this can be repaired following this great guide

We use $IDX for the existing, e.g. broken index and $IDXNEW for the new one.

Show number of records for all indexes

1
curl -X GET http://${ESUSER}:${ESPASS}@localhost:9200/_cat/indices/%2A?v=&s=index:desc

Create new index with mappings

1
2
3
4
5
6
7
8
9
curl -X PUT http://${ESUSER}:${ESPASS}@localhost:9200/$IDXNEW \
  -H 'Content-Type: application/json' \
  -d '{
    "mappings": {
      "properties": {
        [PROVIDE THE MAPPING HERE]
      }
    }
  }'

Re-index from old to new index

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
curl -X POST http://${ESUSER}:${ESPASS}@localhost:9200/_reindex \
  -H 'Content-Type: application/json' \
  -d '{
    "source": {
      "index": "$IDX"
    },
    "dest": {
      "index": "$IDXBEW"
    }
  }'

Delete the old index

1
curl -X DELETE http://${ESUSER}:${ESPASS}@localhost:9200/$IDX

Delete and recreate alias

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
curl -X POST http://${ESUSER}:${ESPASS}@localhost:9200/_aliases \
  -H 'Content-Type: application/json' \
  -d '{
    "actions": [
      {
        "remove": {
          "index": "$IDXNEW",
          "alias": "$IDX"
        }
      }
    ]
  }'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
curl -X POST http://${ESUSER}:${ESPASS}@localhost:9200/_aliases \
  -H 'Content-Type: application/json' \
  -d '{
    "actions": [
      {
        "add": {
          "index": "$IDXNEW",
          "alias": "$IDX"
        }
      }
    ]
  }'

Last update: February 17, 2021 07:08:15