首先建立一个索引:html
POST http://192.168.20.46:9200/my_index/my_type/1 {"name":"zhangsan"}
如今只建立了一个索引,并无设置mapping,查看一下索引mapping的内容:json
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:app
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的内容:code
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信息,再次查看:htm
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修改过程,能够参考.blog