关于elasticsearch:elasticsearch修改索引

什么状况下须要更改ES索引?

在应用ES做我的项目开发的时候,很多时候回随着版本的迭代呈现ES索引数据的变动,如果是在刚开始开发的时候,那就是删除原来的旧数据从新建索引就能够了,然而我的项目上线并经营了一段时间后,很显然这种形式就不可取了。

这时候咱们就要用到ES提供的_reindex办法了

假如ES曾经创立了名叫live的索引,并且在ES外面live曾经存入了很多数据。
mapping构造体为:

{
  "mappings": {
    "properties": {
      "room_id": {
        "type": "keyword"
      },
      "room_name": {
        "type": "text"
      },
      "shop_ids": {
        "type": "keyword"
      },
      "live_date": {
        "type": "keyword"
      },
      "goods_list": {
        "type": "nested",
        "properties": {
          "goods_id": {
            "type": "keyword"
          },
          "goods_name": {
            "type": "text"
          },
          "sku_code": {
            "type": "keyword"
          },
          "category_id": {
            "type": "keyword"
          },
          "activity_type": {
            "type": "keyword"
          },
          "lecture_type": {
            "type": "keyword"
          },
          "area_ids": {
            "type": "keyword"
          }
        }
      },
      "creator_id": {
        "type": "keyword"
      },
      "push_type": {
        "type": "keyword"
      },
      "create_at": {
        "type": "keyword"
      }
    }
  }
}

索引重建以及数据迁徙

假如一个场景,须要更改live对应的mapping构造体,去掉”push_type”字典,另外新增”push_status”字典,然而不影响原来数据。

1.创立一个新的索引,索引名称为datacube_live,索引对应mapping构造体如下所示,通过调用ES接口,创立新索引。

PUT http://localhost:9200/datacub…

{
  "mappings": {
    "properties": {
      "room_id": {
        "type": "keyword"
      },
      "room_name": {
        "type": "text"
      },
      "shop_ids": {
        "type": "keyword"
      },
      "live_date": {
        "type": "keyword"
      },
      "goods_list": {
        "type": "nested",
        "properties": {
          "goods_id": {
            "type": "keyword"
          },
          "goods_name": {
            "type": "text"
          },
          "sku_code": {
            "type": "keyword"
          },
          "category_id": {
            "type": "keyword"
          },
          "activity_type": {
            "type": "keyword"
          },
          "lecture_type": {
            "type": "keyword"
          },
          "area_ids": {
            "type": "keyword"
          }
        }
      },
      "creator_id": {
        "type": "keyword"
      },
      "push_status": {
        "type": "keyword"
      },
      "create_at": {
        "type": "keyword"
      }
    }
  }
}

2.将ES外面live对应的数据,复制到datacube_live索引外面。

POST http://localhost:9200/_reindex

{
  "source": {
    "index": "live"
  },
  "dest": {
    "index": "datacube_live"
  }
}

3.测验迁徙是否胜利

GET http://xxxx:9200/datacube_liv…

申请参数为:
{}

datacube_live曾经蕴含了数据,阐明数据迁徙胜利。能够先删除live索引及数据,再将datacube_live通过重命名,再更改为live,这就实现了整个数据迁徙。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理