映射定义文档如何被存储和检索的
text类型⽤于全⽂索引,搜索时会自动使用分词器进⾏分词再匹配
keyword 不分词,搜索时需要匹配完整的值
创建索引并指定映射
PUT /my_index { "mappings": { "properties": { "age": { "type": "integer" #整数存入如不指定类型会默认为long }, "email": { "type": "keyword" #keyword检索时会精确匹配匹配 }, "name": { "type": "text" #检索时候进行分词匹配 } } } }
创建带检索
添加新的字段映射
PUT /my_index/_mapping { "properties": { "employee-id": { "type": "keyword", "index": false # 字段不能被检索。默认所有字段的index都是true的,只相当于一个冗余存储信息 } } }
更新映射
对于已经存在的字段映射,我们不能更新。更新必须创建新的索引,进行数据迁移。
数据迁移
先创建bank的正确映射newbank
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" : "keyword" }, "gender" : { "type" : "keyword" }, "lastname" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "state" : { "type" : "keyword", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } }
再将bank中的数据迁移到newbank中
POST _reindex { "source": { "index": "bank" ,"type": "account" }, "dest": { "index": "newbank" } }
source指定老数据索引,dest指定新索引
,"type": "account"之所以加了删除线,是因为新版本不需要指定这个类型,6.0之前的老版本需要指定(我试了一下不加也是可以的)
数据迁移及查询
迁移后所有的_type都是_doc了。感觉迁移叫复制比较好,因为老bank数据还存在