S Singh
S Singh

Reputation: 1473

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

Upvotes: 1

Views: 4429

Answers (4)

S Singh
S Singh

Reputation: 1473

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.

Upvotes: 2

S Singh
S Singh

Reputation: 1473

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

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

Upvotes: 1

javanna
javanna

Reputation: 60245

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
}

Upvotes: 2

Plynx
Plynx

Reputation: 11461

If I'm understanding you correctly, just put "_id": whatever into your JSON and ensure index.mapping._id.indexed is set to true.

Upvotes: 1

Related Questions