{"id":10851,"date":"2024-04-01T21:16:53","date_gmt":"2024-04-02T02:16:53","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=10851"},"modified":"2024-04-03T21:23:18","modified_gmt":"2024-04-04T02:23:18","slug":"elasticsearch-to-opensearch-migration-remote-reindex-to-move-data","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=10851","title":{"rendered":"ElasticSearch to OpenSearch Migration: Remote Reindex to Move Data"},"content":{"rendered":"\n<p>Since we cannot do an in-place upgrade of our ElasticSearch environment, I need to <em>move<\/em> everything to the new servers. The biggest component is moving the data &#8212; which can easily be done using the remote reindex. Use the ElasticSearch API to get a list of all indices, and tell the OpenSearch API to reindex that index from the ElasticSearch remote. This operates on deltas &#8212; it will add new documents to an index &#8212; so my plan is to spend a few days seeding the initial data, then perform delta updates leading up to the scheduled change. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport requests\nfrom requests.auth import HTTPBasicAuth\n\nf = open(&quot;results.txt&quot;, &quot;a&quot;)\n\nlistIndexNames = &#x5B;]\n\nreqGetIndexes = requests.get(&#039;https:\/\/elasticsearch.example.com:9200\/_cat\/indices?format=json&#039;, auth=HTTPBasicAuth(&#039;something&#039;,&#039;something&#039;), verify=False)\nfor jsonIndex in reqGetIndexes.json():\n        if jsonIndex.get(&#039;index&#039;)&#x5B;0] != &#039;.&#039;:\n                listIndexNames.append(jsonIndex.get(&#039;index&#039;))\n\nfor strIndexName in listIndexNames:\n  jsonReindexItem = {\n    &quot;source&quot;: {\n      &quot;remote&quot;: {\n        &quot;host&quot;: &quot;https:\/\/elasticsearch.example.com:9200&quot;,\n        &quot;username&quot;: &quot;something&quot;,\n        &quot;password&quot;: &quot;something&quot;\n      },\n  &quot;index&quot;: strIndexName\n    },\n    &quot;dest&quot;: {\n  &quot;index&quot;: strIndexName\n    }\n  }\n\n  r = requests.post(&#039;https:\/\/opensearch.example.com:9200\/_reindex&#039;, json=jsonReindexItem, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n  print(r.json())\n  jsonResponse = r.json()\n\n  if r.status_code == 400 and &quot;mapping set to strict&quot; in jsonResponse.get(&#039;failures&#039;)&#x5B;0].get(&#039;cause&#039;).get(&quot;reason&quot;):\n    # {&#039;error&#039;: {&#039;root_cause&#039;: &#x5B;{&#039;type&#039;: &#039;x_content_parse_exception&#039;, &#039;reason&#039;: &#039;&#x5B;1:2] &#x5B;reindex] unknown field &#x5B;key]&#039;}], &#039;type&#039;: &#039;x_content_parse_exception&#039;, &#039;reason&#039;: &#039;&#x5B;1:2] &#x5B;reindex] unknown field &#x5B;key]&#039;}, &#039;status&#039;: 400}\n    if jsonResponse.get(&#039;failures&#039;):\n      print(jsonResponse.get(&#039;failures&#039;)&#x5B;0].get(&#039;cause&#039;).get(&quot;reason&quot;))\n      print(&quot;I need to set dynamic mapping&quot;)\n      r2 = requests.put(f&#039;https:\/\/opensearch.example.com:9200\/{strIndexName}\/_mapping&#039;, json={&quot;dynamic&quot;:&quot;true&quot;}, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n      print(r2.json)\n      r3 = requests.post(&#039;https:\/\/opensearch.example.com:9200\/_reindex&#039;, json=jsonReindexItem, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something), verify=False)\n      print(r.json())\n      print(f&quot;{strIndexName}\\t{r3.status_code}\\t{r.json()}\\n&quot;)\n      f.write(f&quot;{strIndexName}\\t{r3.status_code}\\t{r.json()}\\n&quot;)\n\n  elif r.status_code == 200:\n    print(jsonResponse)\n    print(f&quot;{strIndexName}\\t{r.status_code}\\t{r.json()}\\n&quot;)\n    f.write(f&quot;{strIndexName}\\t{r.status_code}\\t{r.json()}\\n&quot;)\n  else:\n    print(f&quot;HTTP Error: {r.status_code} on web call&quot;)\n    print(f&quot;{strIndexName}\\t{r.status_code}\\t{r.json()}\\n&quot;)\n    f.write(f&quot;{strIndexName}\\t{r.status_code}\\t{r.json()}\\n&quot;)\n\nf.close()\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Since we cannot do an in-place upgrade of our ElasticSearch environment, I need to move everything to the new servers. The biggest component is moving the data &#8212; which can easily be done using the remote reindex. Use the ElasticSearch API to get a list of all indices, and tell the OpenSearch API to reindex &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1588],"tags":[1590,2001,1740],"class_list":["post-10851","post","type-post","status-publish","format-standard","hentry","category-elk","tag-elasticsearch","tag-elasticsearch-to-opensearch-migration","tag-opensearch"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10851"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10851\/revisions"}],"predecessor-version":[{"id":10852,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10851\/revisions\/10852"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}