首先创建一个索引:
POST http://192.168.20.46:9200/my_index/my_type/1
{"name":"zhangsan"}
现在只创建了一个索引,并没有设置mapping,查看一下索引mapping的内容:
GET http://192.168.20.46:9200/my_index/_mapping?pretty
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
下面给productindex这个索引加一个type,type name为 product 并设置mapping:
PUT http://192.168.20.46:9200/my_index
{
"mappings":{
"product":{
"properties":{
"title":{
"type":"text",
"store":"yes"
},
"description":{
"type":"keyword",
"index":"not_analyzed"
},
"price":{
"type":"double"
},
"onSale":{
"type":"boolean"
},
"type":{
"type":"integer"
},
"createDate":{
"type":"date"
}
}
}
}
}
查看一下索引mapping的内容:
ttp://192.168.20.46:9200/my_index/_mapping?pretty
{
"my_index": {
"mappings": {
"product": {
"properties": {
"createDate": {
"type": "date"
},
"description": {
"type": "keyword"
},
"onSale": {
"type": "boolean"
},
"price": {
"type": "double"
},
"title": {
"type": "text",
"store": true
},
"type": {
"type": "integer"
}
}
}
}
}
}
上面的操作中,我们给productindex加了一个type,并写入了product的mapping信息,再次查看:
PUT http://192.168.20.46:9200/my_index/_mapping/product
{
"product": {
"properties": {
"english_title": {
"type": "string",
"analyzer": "english"
}
}
}
}
GET http://192.168.20.46:9200/my_index/_mapping?pretty
{
"my_index": {
"mappings": {
"product": {
"properties": {
"createDate": {
"type": "date"
},
"description": {
"type": "keyword"
},
"english_title": {
"type": "text",
"analyzer": "english"
},
"onSale": {
"type": "boolean"
},
"price": {
"type": "double"
},
"title": {
"type": "text",
"store": true
},
"type": {
"type": "integer"
}
}
}
}
}
}
字段的type类型是不能修改的,为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型看来是符合lucene机制的。
这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,可以参考.