Search code examples
elasticsearchnestedreindex

ElasticSearch reindex nested field as new documents


I am currently changing my ElasticSearch schema. I previously had one type Product in my index with a nested field Product.users. And I now wants to get 2 different indices, one for Product, an other one for User and make links between both in code.

I use reindex API to reindex all my Product documents to the new index, removing the Product.users field using script:

ctx._source.remove('users');

But I don't know how to reindex all my Product.users documents to the new User index as in script I'll get an ArrayList of users and I want to create one User document for each.

Does anyone knows how to achieve that?


Solution

  • For those who may face this situation, I finally ended up reindexing users nested field using both scroll and bulk APIs.

    • I used scroll API to get batches of Product documents
    • For each batch iterate over those Product documents
    • For each document iterate over Product.users
    • Create a new User document and add it to a bulk
    • Send the bulk when I end iterating over Product batch

    Doing the job <3