javascript - ElasticSearch client.indices.putMapping 总是失败

标签 javascript node.js elasticsearch

我真的不明白这些文档。即使在阅读了一些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/

相关文章:

javascript - 如何在嵌套函数调用中使用 promises

elasticsearch - CQRS为什么不只对读和写都进行elasticsearch?

elasticsearch - 如何在 ElasticSearch 的字段中搜索 "OR"单词?

javascript - 如何通过json服务在node中搭建一个odata服务

javascript - 在 jQuery 中使用 find() 而不是 > 作为子选择器是否更有效?

javascript - 如何在C中建立javascript和cgi之间的通信 channel ?

javascript - Jasmine 测试 postMessage API

javascript - promise 执行错误nodejs javascript

elasticsearch - 如何将 tcp 重定向到 nginx 中 Elasticsearch 服务器的端口 9300?

javascript - 当我调用一个接受参数但不提供这些参数的 Javascript 函数时会发生什么?