【发布时间】:2015-11-16 15:54:14
【问题描述】:
我一直在使用 Elastic Search (ES) 动态映射时遇到问题。好像我在catch-22中。 https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html
主要目标是将进入 ES 的所有内容都存储为字符串。
我尝试过的:
在 ES 中,在索引创建完成之前,您无法创建动态映射 创建的。好吧,有道理。
我无法创建空索引,所以如果 发送到索引的第一项不是字符串,我不能 重新分配它...我不知道什么类型的对象是第一个 索引中的项目,它可以是任何类型,这取决于应用如何接受各种对象/事件。
所以如果不能提前创建mapping,又不能插入空索引来创建mapping,又不能事后改变mapping,第一项怎么处理如果它不是字符串???
这是我目前正在做的事情(使用 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');
});
});
【问题讨论】:
-
你能分享一下你现在正在使用的地图吗?
-
没有映射,这就是我想要做的。创建我自己的自定义一个,其中所有内容都是字符串。我的问题是什么时候创建动态映射,因为我要等到它创建之后才能创建,而且你不能从我读过的内容中更改 ES 中的现有映射,你必须重新索引。
标签: dynamic elasticsearch mapping