本帖仅针对ElasticSearch5.02版本实现,其他版本请自行参考官网API文档。
public static boolean updateMapping(Client client,String indexName,String indexType,String mappingJson){
try {
JSONObject json = JSON.parseObject(mappingJson);
PutMappingRequest putRequest = new PutMappingRequest(indexName);
putRequest.source(json);
putRequest.type(indexType);
PutMappingResponse response = client.admin().indices().putMapping(putRequest).actionGet();
if(!response.isAcknowledged()){
Log.Log2File(Log.LEVEL_ERROR, CLASS_NAME, "update [%s] mapping [%s] failed !",indexName,json);
return false;
} else {
Log.Log2File(Log.LEVEL_INFO, CLASS_NAME, "update [%s] mapping [%s] successfully !",indexName,json);
}
}catch(Exception e){
Log.Log2File(Log.LEVEL_ERROR, CLASS_NAME, "Exception: %s", DebugUtil.PrintExcepFullMessage(e));
return false;
}
return true;
}
其中mappingJson的示例如下:
{
"properties": {
"test_name": {
"type": "keyword",
"index": "not_analyzed",
"doc_values": "false",
"ignore_above": 2048
}
}
}
cerebro等客户端操作
PUT test/_mapping
{
"properties": {
"text": {
"type": "text"
},
"flag": {
"type": "text",
"analyzer": "keyword"
}
}
}
使用curl操作
curl -X PUT "localhost:9200/test/_mapping?pretty" -H 'Content-Type: application/json' -d '
{
"properties": {
"text": {
"type": "text"
},
"flag": {
"type": "text",
"analyzer": "keyword"
}
}
}'