我正在尝试创建一个索引,该索引将在每次重新创建时具有相同的结构。 我已经在ES上创建了一个模板,并希望在通过Java程序创建和填充索引时使用它。 通过Java API创建索引时如何使用索引模板。
索引模板
PUT _template/quick-search
{
"index_patterns": ["quick-search*"],
"settings": {
"number_of_shards": 1
},
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"item" : {
"type" : "long"
},
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
答案 0 :(得分:0)
由于添加了索引模板,因此,如果现在尝试将文档保存在不存在的索引上,并且其名称将与模板"index_patterns": ["quick-search*"]
中提供的模式相匹配,elasticsearch会根据模板自动创建映射,而不是根据您的输入创建映射。
此外,如果尝试创建新索引并尝试设置映射(再次匹配模式),则该模板将作为默认模板。因此,对于新索引,可以将keyword
的类型设置为item
。这将覆盖模板long
的默认设置,但其他设置将从模板中获取,并且也将出现。
要进行测试,请转到kibana并设置模板
PUT _template/quick-search
{
"index_patterns": [
"quick-search*"
],
"settings": {
"number_of_shards": 1
},
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"item": {
"type": "long"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
然后尝试创建新索引,但项目必须是关键字
PUT quick-search-item-keyword
{
"mappings": {
"properties": {
"item": {
"type": "keyword",
"index": true
}
}
}
}
GET quick-search-item-keyword/_mapping
现在只需将文档保存在不存在的索引中
POST quick-search-test-id/_doc/
{
"id": "test"
}
GET quick-search-test-id/_mapping
尝试将同一文档保存在不存在且与模板的索引模式不匹配的索引中
POST test/_doc/
{
"id": "test"
}
GET test/_mapping
如果使用Java客户端,没有什么不同。