【问题标题】:NodeJS - Elastic Search for insert data to indexNodeJS - 将数据插入索引的弹性搜索
【发布时间】:2020-02-12 18:09:43
【问题描述】:

我关注this link获取弹性搜索索引的映射,并想在其中插入数据(不包括所有字段),如下所示,但失败了。

索引的映射:

{
    "mymapping": {
        "mappings": {
            "_meta": {
                "beat": "apm",
                "version": "7.5.0"
            },
            "dynamic_templates": [
                {
                    "labels": {
                        "path_match": "labels.*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "keyword"
                        }
                    }
                }
            ],
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "people": {
                    "dynamic": "false",
                    "properties": {
                        "id": {
                            "type": "keyword",
                            "ignore_above": 1024
                        },
                        "name": {
                            "type": "keyword",
                            "ignore_above": 1024
                        }
                    }
                }
            }
        }
    }
}

我准备好client.js没有问题,这里是InsertData.js:

const esClient = require('./client');
const insertDoc = async function(indexName, _id, mappingType, data){
    return await esClient.index({
        index: indexName,
        type: mappingType,
        id: _id,
        body: data
    });
}

module.exports = insertDoc;
async function test(){
    const data = {
        beat: 'apm',
        version: '7.5.0'
    }
    try {
        const resp = await insertDoc('mymapping', 2, '_meta', data);
        console.log(resp);
    } catch (e) {
        console.log(e);
    }
}
test();

当我尝试插入数据时,出现异常。 错误输出:

message:  
'[illegal_argument_exception] Rejecting mapping update to [mymapping] as the final mapping would have more than 1 type: [_doc, _meta]',  
path: '/mymapping/_doc/2',  
query: { type: '_meta' },  
body:  
{ error:  
    { root_cause: [Array],  
    type: 'illegal_argument_exception',  
    reason:  
        'Rejecting mapping update to [mymapping] as the final mapping would have more than 1 type: [_doc, _meta]' },  
    status: 400 },  
statusCode: 400,  
response:  
'{  
    "error": {  
        "root_cause": [  
            {  
                "type": "illegal_argument_exception",  
                "reason": "Rejecting mapping update to [mymapping] as the final mapping would have more than 1 type: [_doc, _meta]"  
            }  
        ],  
        "type": "illegal_argument_exception",  
        "reason": "Rejecting mapping update to [mymapping] as the final mapping would have more than 1 type: [_doc, _meta]"  
    },  
    "status": 400  
}'  

如何正确插入数据?

【问题讨论】:

    标签: node.js elasticsearch insert


    【解决方案1】:

    启动 elasticsearch 6+ 多种类型已被弃用。您试图在创建元映射时显式放置类型,默认插入的映射是_doc

    更多详情请参见:Removal of types

    【讨论】:

      猜你喜欢
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多