简体   繁体   中英

node.js: data indexing in elasticsearch by using user defined id

I need to index multiple jsons in elasticsearch and indexing id should be given by user not by automatically created by elasticserach.

Can any please tell me how to stop elasticsearch from creating automatic index id and how can I use my desired id for data indexing.

Below is the part of node.js code for indexing data:

elasticSearchClient.index('index_name', 'type', json)
                .on('data', function(data) {
                    console.log("************ "+data+" ****************")
                })
                .exec()

Any help will be greatly appreciated!

Regards

Just include the id field in your json document. It will be automatically extracted from the document and put in the url. In fact the core.js script contains this logic:

if (document.id) {
    path += "/" + document.id
    method = 'PUT'
    delete document.id
}

If we don't give indexing id then indexing id for document will be auto created by elasticsearch.

So I use below code and tell the indexing id for indexing doc.

var commands = []
 commands.push({ "index" : { "_index" :'opt', "_type" : "art", "_id":my_id} })
 commands.push(index_ingjson_doc)

 elasticSearchClient.bulk(commands, {})
                            .on('data', function(data) {
                            }).on('error', function(error){})
                            .exec();

In this way, I resolved my problem! Some other solutions may be possible but as of now I am using above code.

如果我对您的理解正确,只需将"_id": whatever放入JSON并确保将index.mapping._id.indexed设置为true

elasticSearchClient.bulk() is the solution for my problem.

Reference: https://github.com/phillro/node-elasticsearch-client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM