elasticsearch创建索引带mappings和settings
一、通过kabana控制台创建
我们在kabana控制台创建一个record_feature_tag的索引,对应的mapping配置如下
PUT /record_feature_tag
{
"mappings": {
"properties" : {
"_class" : {
"type" : "keyword"
},
"api" : {
"type" : "keyword"
},
"app_name" : {
"type" : "keyword"
},
"gmt_create" : {
"type" : "long"
},
"has_case" : {
"type" : "boolean"
},
"source_id" : {
"type" : "keyword"
},
"sum" : {
"type" : "long"
},
"tagMap" : {
"properties" : {
"app_id" : {
"type" : "keyword"
},
"group_type" : {
"type" : "keyword"
},
"method" : {
"type" : "keyword"
},
"origin_app_id" : {
"type" : "keyword"
},
"result_code" : {
"type" : "keyword"
}
}
},
"timestamp" : {
"type" : "date"
}
}
},
"settings": {
"number_of_shards": 3
}
}
二、通过代码创建
@Slf4j
public class Test {
String recordMappings = "{\n" +
" "properties": {\n" +
" "_class": {\n" +
" "type": "keyword",\n" +
" "index": false,\n" +
" "doc_values": false\n" +
" },\n" +
" "agg_api": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "api": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "app_name": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "async": {\n" +
" "type": "long"\n" +
" },\n" +
" "caseSet": {\n" +
" "type": "text"\n" +
" },\n" +
" "entrance": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "env": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "git_version": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "gmtRecord": {\n" +
" "type": "long"\n" +
" },\n" +
" "gmt_create": {\n" +
" "type": "long"\n" +
" },\n" +
" "host": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "id": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "instance_name": {\n" +
" "type": "text",\n" +
" "index": false\n" +
" },\n" +
" "recordType": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "request": {\n" +
" "type": "text",\n" +
" "index": false\n" +
" },\n" +
" "requestType": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "response": {\n" +
" "type": "text",\n" +
" "index": false\n" +
" },\n" +
" "subInvocationBrief": {\n" +
" "type": "keyword",\n" +
" "index": false\n" +
" },\n" +
" "tags": {\n" +
" "properties": {\n" +
" "id": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "label": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "show": {\n" +
" "type": "boolean"\n" +
" },\n" +
" "value": {\n" +
" "type": "keyword"\n" +
" }\n" +
" }\n" +
" },\n" +
" "trace_id": {\n" +
" "type": "keyword"\n" +
" },\n" +
" "version": {\n" +
" "type": "integer"\n" +
" },\n" +
" "wrapper_record": {\n" +
" "type": "keyword",\n" +
" "index": false\n" +
" }\n" +
" }\n" +
"}";
@Resource(name = "elasticSearchRestTemplate")
ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
* 创建索引
* @param indexName
*/
public void createIndex(String indexName) {
Map<String, Object> settings = new HashMap<>();
settings.put("index.number_of_shards", 2);
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(indexName);
if (indexOperations.exists()) {
return;
}
try {
Document mapping = Document.parse(recordMappings);
if (!indexOperations.exists()) {
indexOperations.create(settings, mapping);
}
} catch (Exception e) {
log.error("system error", e);
}
}
}