gpt4 book ai didi

elasticsearch - 使用Java API创建索引时如何使用模板

转载 作者:行者123 更新时间:2023-12-03 02:24:42 31 4
gpt4 key购买 nike

我正在尝试创建一个索引,该索引在每次重新创建时都将具有共同的结构。
我已经在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
}
}
}
}
}
}

最佳答案

由于已经添加了索引模板,因此,如果现在尝试将文档保存在不存在的索引上,并且其名称将与您在模板"index_patterns": ["quick-search*"]中提供的模式相匹配,elasticsearch将根据以下内容自动创建映射模板,而不是根据您的输入创建映射。

此外,如果尝试创建新索引并尝试设置映射(再次匹配模式),则该模板将用作默认模板。因此,对于新索引,可以将类型设置为keyworditem。这将覆盖模板的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
}
}
}
}
}
}

然后尝试创建一个新索引,但item必须是关键字
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客户端,没有什么不同。同样适用

关于elasticsearch - 使用Java API创建索引时如何使用模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61372865/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com