putMapping elasticsearch index未找到异常

时间:2016-11-11 15:58:08

标签: node.js elasticsearch

我只是尝试使用putMapping创建索引,并且相当确定这是一个语法问题。弹性客户端正在为.index和.search命令工作,所以我不相信它的设置是个问题。相反,它与我的putmapping代码有关。任何建议将不胜感激。我想要做的一点描述。我搜索了一个索引,发现它不存在。我捕获错误并测试它是否等于“index_not_found_exception”。如果是这样,我需要创建索引。 “...”以下的所有内容都在.catch中。

"use strict";
let config = require("./config"),
    soap = require("soap"),
    elastic = require("elasticsearch").Client(config.routeHost);
...
 if (error.body.error.type == "index_not_found_exception") {
        console.log("no index");
        elastic.indices.putMapping({
            index: "ppvevents",
            type: "ppvObject",
            body: {
                properties: {
                    name: {type: "string", index: "not_analyzed"},
                    hdid: {type: "integer"},
                    sdid: {type: "integer"}
                }
            }
        }).then(function(response){
            console.log("Response: ", response);
        }).catch(function(error){
            console.log("putMapping Error: ", error);
        });
    } else if(error.body.error.type != "index_not_found_exception") {
        console.log("error: elasticsearch client search");
        console.log(error);
    }

控制台响应如下:

vagrant at localhost in /vagrant on master!
± node index.js
9000
no index
putMapping Error:  { [Error: [index_not_found_exception] no such index, with { resource.type=index_or_alias resource.id=ppvevents index=ppvevents }]
    status: 404,
    displayName: 'NotFound',
    message: '[index_not_found_exception] no such index, with { resource.type=index_or_alias resource.id=ppvevents index=ppvevents }',
    path: '/ppvevents/_mapping/ppvObject',
    query: {},
    body:
        { error:
            { root_cause: [Object],
                type: 'index_not_found_exception',
                reason: 'no such index',
                'resource.type': 'index_or_alias',
                'resource.id': 'ppvevents',
                index: 'ppvevents' },
            status: 404 },
        statusCode: 404,
        response: '{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"ppvevents","index":"ppvevents"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"ppvevents","index":"ppvevents"},"status":404}',
toString: [Function],
toJSON: [Function] }

2 个答案:

答案 0 :(得分:2)

如果索引不存在,putMapping()无效,则需要拨打indices.createputMapping只能用于向已存在的索引添加新的映射类型。

将putMapping调用替换为:

    elastic.indices.createIndex({
        index: "ppvevents",
        body: {
            mappings: {
               ppvObject: {
                  properties: {
                     name: {type: "string", index: "not_analyzed"},
                     hdid: {type: "integer"},
                     sdid: {type: "integer"}
                  }
               }
            }
        }

答案 1 :(得分:0)

新版elasticsearch.js已经去掉了elastic.indices.createIndex,需要使用client.indices.create