1

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

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

Comments

1

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

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

1 Comment

Can you explain why? I don't get how your answer is related to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.