我真的不明白这些文档。即使在阅读了一些SO问题和答案之后(例如: this one )。我有这个代码
let indexMapping = {
'foo': {
name: { type: 'string', index: 'analyzed' },
value: { index: 'no' },
dynamic_templates: [ {
customRule: {
path_match: 'bar.*',
mapping: { type: '{dynamic_type}', index: 'no' }
}
} ]
}
};
let indexName = 'foos';
let typeName = 'foo';
client.indices.putMapping({
index: indexName,
type: typeName,
[typeName]: {
properties: indexMapping[typeName]
}
}).catch(function (err) {
console.error(err.stack || err);
});
我总是得到
Error: [action_request_validation_exception] Validation Failed: 1: mapping source is empty;
我做错了什么?
更多信息
我的索引是新创建的,当前没有添加任何文档或定义任何类型。这是一个新的空白索引,我想在添加文档和索引文档之前将其设置为映射。
最佳答案
动态模板声明应与映射类型的属性
处于同一级别,因此您的代码应如下所示:
let indexName = 'foos';
let typeName = 'foo';
let indexMapping = {
'properties': {
[typeName]: {
name: { type: 'string', index: 'analyzed' },
value: { type: 'string', index: 'no' }
}
},
'dynamic_templates': [ {
customRule: {
path_match: 'bar.*',
mapping: { type: '{dynamic_type}', index: 'no' }
}
} ]
};
client.indices.putMapping({
index: indexName,
type: typeName,
body: indexMapping
}).catch(function (err) {
console.error(err.stack || err);
});
关于javascript - ElasticSearch client.indices.putMapping 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708006/