ES映射关系的迁移

ES中的type相当于数据库中‘表’的概念,但由于底层Lucene缺乏支持,造成不同的type只要字段名相同,映射关系都是一样的,正因如此,ES将来的版本会取消type的概念,变成index(索引)-document(文档)的二层关系。下面来对老的映射关系做迁移。

1、查询bank索引,可以观察到type为account

2、调用GET /bank/_mapping,获取bank索引的映射

{

  "bank" : {

    "mappings" : {

      "properties" : {

        "account_number" : {

          "type" : "long"

        },

        "address" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "age" : {

          "type" : "long"

        },

        "balance" : {

          "type" : "long"

        },

        "city" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "email" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "employer" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "firstname" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "gender" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "lastname" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        },

        "state" : {

          "type" : "text",

          "fields" : {

            "keyword" : {

              "type" : "keyword",

              "ignore_above" : 256

            }

          }

        }

      }

    }

  }

}

3、对映射关系进行优化、

1)、新的映射关系需要删除type层级;

2)、type为text类型时,代表可以进行全文检索,但只对需要全文检索的字段进行设置,如需精确匹配,比如城市、性别等保存固定值的字段,可以设置成keyword;

3)、type类型下附加keyword类型,代表该字段既可以被全文检索,又可以被精确检索,同样只对需要的字段进行设置;

4、但老的映射关系不可被更新,只能被替换,故新建映射关系

PUT /newbank

{

  "mappings": {

    "properties": {

      "account_number": { "type": "long" },

      "address": { "type": "text" },

      "age": { "type": "integer" },

      "balance": { "type": "long" },

      "city": { "type": "keyword" },

      "email": { "type": "keyword" },

      "employer": { "type": "keyword" },

      "firstname": { "type": "text" },

      "gender": { "type": "keyword" },

      "lastname": {

        "type": "text",

        "fields": {

          "keyword": {

            "type": "keyword",

            "ignore_above": 256

          }

        }

      },

      "state": { "type": "keyword" }

    }

  }

}

5、调用reindex的API接口,对bank索引进行重新映射。source字段为原索引和索引类型,dest字段为目标索引,因为要去除type所以没有定义该字段;

POST _reindex

{

  "source": {

    "index": "bank",

    "type": "account"

  },

  "dest": {

    "index": "newbank"

  }

}

6、调用_mapping接口,确认新映射关系已被替换

7、重新检索bank索引,可以观察到type已经变为缺省类型

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容