一、新建文档映射
通过 Elasticsearch API 可以向索引(Index) 添加文档类型(Type), 或者向文档类型(Type) 中添加/更新字段(Field)。
PUT http://127.0.0.1:9200/commodity
{
"mappings": {
"properties": {
"category": {
"type": "text"
},
"slsj": {
"format": "yyyy-MM-dd HH:mm:ss",
"store": true,
"type": "date"
},
"content": {
"analyzer": "icu_analyzer",
"type": "text"
},
"serialno": {
"type": "text"
}
}
}
}
提示如下信息就是添加文档成功。
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "msg_sort"
}
二、向已存在的类型中添加字段
commodity 索引创建成功后,我们还可以向其中添加新的字段,下面添加一个 price(价格)字段:
PUT http://127.0.0.1:9200/commodity/_mapping/
{
"properties": {
"price": {
"type": "double"
}
}
}
三、同时向多个索引设置映射
先创建两个未设置映射的空索引 commodity1、commodity2:
PUT http://127.0.0.1:9200/commodity1
PUT http://127.0.0.1:9200/commodity1
然后向这两个索引同时添加映射:
PUT http://localhost:9200/commodity1,commodity2/_mapping/
{
"properties": {
"commodity_id": {
"type": "long"
},
"commodity_name": {
"type": "text"
},
"picture_url": {
"type": "keyword"
}
}
}