import '../../util/catchUnhandledRejection';
import config from 'config';
import elasticsearch from 'elasticsearch';

import getIndexName from '../../util/getIndexName';

const client = new elasticsearch.Client({
  host: config.get('ELASTICSEARCH_URL'),
  log: 'trace',
});

async function main() {

  client.reindex({
    waitForCompletion: true,
    body: {
      source: { index: 'articles_legacy' },
      dest: {
        index: getIndexName('articles'),
        type: 'doc',
        op_type: 'create',
      },
      conflicts: 'proceed',
      script: {
        lang: 'painless',
        source: `
          ctx._source.put('articleReplies', new ArrayList());
          ctx._source.put('hyperlinks', new ArrayList());
          ctx._source.put('tags', new ArrayList());
          ctx._source.put('replyRequestCount', ctx._source.replyRequestIds.size());

          ctx._source.put('app_id', ctx._source.from);
          ctx._source.remove('from');
        `,
      },
    },
  });

  client.reindex({
    waitForCompletion: true,
    body: {
      source: { index: 'replies_legacy' },
      dest: {
        index: getIndexName('replies'),
        type: 'doc',
        op_type: 'create',
      },
      conflicts: 'proceed',
      script: {
        lang: 'painless',
        source: `
          ctx._source.put('hyperlinks', new ArrayList());

          ctx._source.putAll(ctx._source.versions[0]);
          ctx._source.remove('versions');

          ctx._source.put('app_id', ctx._source.from);
          ctx._source.remove('from');
        `,
      },
    },
  });
}

main();
@@ -292,18 +292,23 @@
 ain() %7B%0A
-%0A
  
+ await
  client.
@@ -881,34 +881,33 @@
 _source.put('app
-_i
+I
 d', ctx._source.
@@ -987,16 +987,22 @@
  %7D);%0A%0A  
+await 
 client.r
@@ -1484,10 +1484,962 @@
 'app
-_i
+Id', ctx._source.from);%0A          ctx._source.remove('from');%0A        %60,%0A      %7D,%0A    %7D,%0A  %7D);%0A%0A  await client.reindex(%7B%0A    waitForCompletion: true,%0A    body: %7B%0A      source: %7B index: 'replyconnectionfeedbacks_legacy' %7D,%0A      dest: %7B%0A        index: getIndexName('articlereplyfeedbacks'),%0A        type: 'doc',%0A        op_type: 'create',%0A      %7D,%0A      conflicts: 'proceed',%0A      script: %7B%0A        lang: 'painless',%0A        source: %60%0A          ctx._source.remove('comment');%0A%0A          ctx._source.put('appId', ctx._source.from);%0A          ctx._source.remove('from');%0A        %60,%0A      %7D,%0A    %7D,%0A  %7D);%0A%0A  await client.reindex(%7B%0A    waitForCompletion: true,%0A    body: %7B%0A      source: %7B index: 'replyrequests_legacy' %7D,%0A      dest: %7B%0A        index: getIndexName('replyrequests'),%0A        type: 'doc',%0A        op_type: 'create',%0A      %7D,%0A      conflicts: 'proceed',%0A      script: %7B%0A        lang: 'painless',%0A        source: %60%0A          ctx._source.put('appI
 d', 
@@ -2535,11 +2535,12 @@
 %0A%7D%0A%0A
-
 main();
+%0A

Add reindex script for articlereplyfeedbacks and replyrequests