ElasticSearch client.indices.putMapping总是失败

我真的不明白这个文件。 即使读了几个SO问题和答案(例如: 这一个 )。 我有这个代码

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); }); 

我总是得到

错误:[action_request_validation_exception]validation失败:1:映射源为空;

我究竟做错了什么?

更多信息

我的索引是新创build的,没有添加文档或当前定义的任何types。 这是一个新的空白索引,我想在添加和索引文档之前设置映射。

dynamic模板声明应该和映射types的properties在同一级别,所以你的代码应该看起来像这样:

 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); }); 

你使用的语法有点不合适,最后的调用应该是这样的:

 client.indices.putMapping({ index: 'foos', type: 'foo', body: { "foo": { properties: { name: { type: 'string', index: 'analyzed' }, value: { index: 'no' }, dynamic_templates: [{ customRule: { path_match: 'bar.*', mapping: { type: '{dynamic_type}', index: 'no' } } }] } } } }).catch(function(err) { console.error(err.stack || err); }); 

另外请注意,您必须像为name一样定义value的types,而dynamic模板部分也缺lessfields对象。