无法使用Node.js创建弹性搜索索引

时间:2019-07-12 15:51:25

标签: node.js elasticsearch

我尝试在node.js中创建elasticsearch索引,但总是遇到异常:

status: 405,
  displayName: 'MethodNotAllowed',
  message:
   'Incorrect HTTP method for uri [/anil] and method [POST], allowed: [DELETE, PUT, GET, HEAD]' } { error:
   'Incorrect HTTP method for uri [/anil] and method [POST], allowed: [DELETE, PUT, GET, HEAD]',
  status: 405 } 405

搜索,获取映射,索引存在功能可以正常工作,但不能创建索引。

(function () {
    const elasticsearch = require('elasticsearch');
    const esClient = new elasticsearch.Client({
        host: '127.0.0.1:9200',
        log: 'error'
    });
    const indices = function indices() {
        esClient.indices.exists({
            index: 'anil'
        }, (err, res, status) = > {
            if (res) {
                console.log('index already exists');
            } else {
                //tring to create index but not working
                esClient.indices.create({
                    index: 'anil'
                }, (err, res, status) = > {
                    console.log(err, res, status);
                })
            }
        });
    };
    // only for testing purposes
    // all calls should be initiated through the module
    const test = function test() {
        console.log(`elasticsearch indices information: `);
        indices();
    };
    test();
    module.exports = {
        indices
    };
}());

请帮助我如何使用Node.js创建Elasticsearch索引

1 个答案:

答案 0 :(得分:0)

尝试通过添加以下行来更改 elasticsearch 配置,然后重新启动 elasticsearch 并重试:

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE