Elasticsearch中怎么对mapping进行修改

发布时间:2021-06-22 17:37:00 作者:Leah
来源:亿速云 阅读:604

Elasticsearch中怎么对mapping进行修改,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

旧索引信息如下:

index:test_v1

type:item

alias:item_alias

mapping:

{
  "properties": {
    "itemId": {
      "type": "long"
    },
    "itemName": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    }
  }
}

添加字段:

使用最新的mapping直接调用putMapping接口

PUT http://xxx.xxx.xxx.xxx:9200/goods_info/_mapping
{
  "properties": {
    "itemId": {
      "type": "long"
    },
    "itemName": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    },
    "brandName": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    }
  }
}

修改字段:

      由于Elasticsearch底层使用了lucene的原因,不支持对mapping的修改,可使用索引重建的方式,步骤如下:

1,使用正确的mapping新建索引和类型

     如需要将旧索引的itemId字段改为keyword类型,则执行以下请求:

创建index:

PUT /test_v2

设置mapping:

POST /test_v2/item/_mapping

{

  "properties": {
    "itemId": {
      "type": "keyword"
    },
    "itemName": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_smart"
    }
  }
}

2,使用reindex api将旧索引数据导入新索引

索引重建后可使用reindex命令迁移数据,如将test_v1数据迁移至test_v2请求如下:

POST _reindex

{
  "source": {
    "index": "test_v1",
    "type": "item"
  },
  "dest": {
    "index": "test_v2",
    "type": "item"
  }
}

3,为新索引添加别名

      为索引添加别名后,在程序代码中可以使用固定别名查询动态的索引名称,然后进行查询,如此索引重建则不会引起程序的变动

添加别名请求:

POST /_aliases

{
    "actions": [
        { "add": {
            "alias": "item_alias",
            "index": "test_v2"
        }}
    ]
}

将旧索引别名迁移到新索引请求:
  

POST /_aliases

{
    "actions" : [
        { "remove" : { "index" : "test_v1", "alias" : "item_alias" } },
        { "add" : { "index" : "test_v2", "alias" : "item_alias" } }
    ]
}

看完上述内容,你们掌握Elasticsearch中怎么对mapping进行修改的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 使用java怎么对elasticsearch进行操作
  2. Hibernate中怎么对addMate进行修改

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch lucene

上一篇:Elasticsearch中怎么添加新字段

下一篇:python中怎么实现异步url请求

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》