创建 mapping 模板 组件
PUT _component_template/student_template_mapping_001
{
"template": {
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"key": {
"type": "keyword"
}
}
},
"year": {
"type": "integer",
"fields": {
"key": {
"type": "keyword"
}
}
}
}
}
}
}
创建 setting 模板组件
PUT _component_template/student_template_setting_001
{
"template": {
"settings": {
"number_of_shards": 3
}
}
}
创建 alias 模板组件
PUT _component_template/student_template_alias_001
{
"template": {
"aliases": {
"student": {
"filter": {
"term": {
"year": "1"
}
}
}
}
}
}
创建索引模板
PUT _index_template/student_template_002
{
"index_patterns": [
"student-*"
],
"composed_of": [
"student_template_setting_001",
"student_template_mapping_001",
"student_template_alias_001"
]
}
查看索引模板组件
GET _component_template
查看索引模板
添加数据
PUT student-001/_doc/1?routing=1
{
"id": 1,
"name": "student-a",
"year": 1
}
PUT student-001/_doc/2?routing=2
{
"id": 2,
"name": "student-b",
"year": 2
}
PUT student-001/_doc/3?routing=3
{
"id": 3,
"name": "student-c",
"year": 3
}
PUT student-001/_doc/4?routing=3
{
"id": 4,
"name": "student-c",
"year": 1
}
成功查询到数据:
GET student_year_1/_search
查询结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student-001",
"_type" : "_doc",
"_id" : "6-nnM3gB_jopoKW0Srd_",
"_score" : 1.0,
"_source" : {
"id" : 5,
"name" : "student-5",
"year" : 1
}
},
{
"_index" : "student-001",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_routing" : "3",
"_source" : {
"id" : 4,
"name" : "student-c",
"year" : 1
}
},
{
"_index" : "student-001",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"id" : 1,
"name" : "student-a",
"year" : 1
}
}
]
}
}