elasticsearchelasticsearch-opendistro

Add _id to the source as a separate field to all exist docs in index


I'm new to Elastic Search. I need go through all the documents, take the _id and add it to the _source as a separate field by script. Is it possible? If yes, сan I have an example of something similar or a link to similar scripts? I haven't seen anything like that on the docks. Why i need it? - Because after that i will do SELECT with Opendistro and SQL. This frame cannot return me fields witch not in source. If anyone can suggest I would be very grateful.


Solution

  • There are two options:

    First option: Add this new field in your existing index and populate it and build the new index again.

    Second option: Simply define a new field in a new index mapping(keep rest all field same) and than use reindex API with below script.

     "script": {
            "source": "ctx._source.<your-field-name> = ctx._id"
        }