index rebuild for elasticsearch

     We know that es cannot modify the value of the mapping again after the mapping of the field is established. In our actual situation, sometimes it is necessary to modify the value of the mapping, and the solution is to rebuild the index data.

Method 1 :

    Use index aliases, create another index, use scroll to search and insert data, etc. ( there are many such examples on the Internet, omitted )

Method 2 : (Reference link: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html)

    Use es' _reindex api to solve it.

    Note: The data of the snapshot of the index obtained by _reindex means that some data may be lost during the process of rebuilding the index

    Demonstration steps for index rebuild:

    1. Create oldindex

    2. Create an alias for the index

    3. I want to insert 9 pieces of data into oldindex

    4. Create a new index newindex

    5. Rebuild the index

    6. Implement the switch without restarting the service index

------------------------------------------------------------------------------------------------------------------------------

1. Create oldindex

curl -XPUT "http://192.168.99.1:9200/oldindex" -d'
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "product" : {
      "properties": {
        "name" : {
          "type": "text"
        },
        "price" : {
          "type": "double"
        }
      }
    }
  }
}'

2. Create an alias for the index

curl -XPUT "http://192.168.99.1:9200/oldindex/_alias/alias_oldindex"

3. I want to insert 9 pieces of data into oldindex

curl -XPOST "http://192.168.99.1:9200/alias_oldindex/product/_bulk" -d'
{"create":{"_id":1}}
{"name":"name 01","price":1}
{"create":{"_id":2}}
{"name":"name 02","price":2}
{"create":{"_id":3}}
{"name":"name 03","price":3}
{"create":{"_id":4}}
{"name":"name 04","price":4}
{"create":{"_id":5}}
{"name":"name 05","price":5}
{"create":{"_id":6}}
{"name":"name 06","price":6}
{"create":{"_id":7}}
{"name":"name 07","price":7}
{"create":{"_id":8}}
{"name":"name 08","price":8}
{"create":{"_id":9}}
{"name":"name 09","price":9}
'

4. Create a new index newindex

curl -XPUT "http://192.168.99.1:9200/newindex" -d'
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "product" : {
      "properties": {
        "name" : {
          "type": "keyword"
        },
        "price" : {
          "type": "double"
        }
      }
    }
  }
}'

5. Rebuilding the index 
    _source means only inserting the data of the price field of the old index into the new index.

6. Implement the switch without restarting the service index

curl -XPOST "http://192.168.99.1:9200/_aliases" -d'
{
  "actions": [
    {
      "remove": {
        "index": "oldindex",
        "alias": "alias_oldindex"
      }
    },
    {
      "add": {
        "index": "newindex",
        "alias": "alias_oldindex"
      }
    }
  ]
}'

    The above completes the process of index reconstruction, and realizes that the newly created index can be applied without stopping the service. ( Premise: The alias of the index is used in the program )

    For more complex usage, refer to the URL: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326980333&siteId=291194637