gpt4 book ai didi

dynamic - Elastic Search 何时添加动态映射

转载 作者:行者123 更新时间:2023-12-02 22:24:28 25 4
gpt4 key购买 nike

我一直在使用 Elastic Search (ES) 动态映射时遇到问题。好像我在第 22 条军规中。 https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html

主要目标是将进入 ES 的所有内容存储为字符串

我尝试过的:

  1. 在 ES 中,你不能创建一个动态映射,直到索引被创建创建。好的,有道理。

  2. 我不能创建空索引,所以如果 发送到索引中的第一项不是字符串,我不能 重新分配它...我不知道第一个是什么类型的对象 索引中的项目,它可以是任何类型,这取决于应用程序接受各种对象/事件的方式。

所以如果不能提前创建映射,不能插入空索引创建映射,不能事后改变映射,我该如何处理第一项如果它不是字符串???

这是我目前正在做的事情(使用 Javascript 客户端)。

createESIndex = function (esClient){
esClient.index({
index: 'timeline-2015-11-21',
type: 'event',
body: event
},function (error, response) {
if (error) {
logger.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
console.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
res.status(500).send('Error saving document');
} else {
res.status(200).send('Accepted');
}
});
}

esClientLookup.getClient( function(esClient) {

esClient.indices.putTemplate({
name: "timeline-mapping-template",
body:{
"template": "timeline-*",
"mappings": {
"event": {
"dynamic_templates": [
{ "timestamp-only": {
"match": "@timestamp",
"match_mapping_type": "date",
"mapping": {
"type": "date",
}
}},
{ "all-others": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
}
}
}
]
}
}
}
}).then(function(res){
console.log("put template response: " + JSON.stringify(res));
createESIndex(esClient);

}, function(error){
console.log(error);
res.status(500).send('Error saving document');
});
});

最佳答案

Index templates救援!这正是您所需要的,想法是创建一个索引模板,一旦您希望将文档存储在该索引中,ES 就会使用您提供的映射(甚至是动态映射)为您创建它

curl -XPUT localhost:9200/_template/my_template -d '{
"template": "index_name_*",
"settings": {
"number_of_shards": 1
},
"mappings": {
"type_name": {
"dynamic_templates": [
{
"strings": {
"match": "*",
"match_mapping_type": "*",
"mapping": {
"type": "string"
}
}
}
],
"properties": {}
}
}
}'

然后,当您在名称与 index_name_* 匹配的索引中索引任何内容时,将使用上面的动态映射创建索引。

例如:

curl -XPUT localhost:9200/index_name_1/type_name/1 -d '{
"one": 1,
"two": "two",
"three": true
}'

这将创建一个名为 index_name_1 的新索引,其映射类型为 type_name,其中所有属性均为 string。你可以用

验证
curl -XGET localhost:9200/index_name_1/_mapping/type_name

响应:

{
"index_name_1" : {
"mappings" : {
"type_name" : {
"dynamic_templates" : [ {
"strings" : {
"mapping" : {
"type" : "string"
},
"match" : "*",
"match_mapping_type" : "*"
}
} ],
"properties" : {
"one" : {
"type" : "string"
},
"three" : {
"type" : "string"
},
"two" : {
"type" : "string"
}
}
}
}
}
}

请注意,如果您愿意通过 Javascript API 执行此操作,则可以使用 indices.putTemplate打电话。

关于dynamic - Elastic Search 何时添加动态映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739425/

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