{"id":10860,"date":"2024-03-29T21:32:40","date_gmt":"2024-03-30T02:32:40","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=10860"},"modified":"2024-04-03T21:41:50","modified_gmt":"2024-04-04T02:41:50","slug":"elasticsearch-to-opensearch-migration-creating-index-templates","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=10860","title":{"rendered":"ElasticSearch to OpenSearch Migration: Creating Index Templates"},"content":{"rendered":"\n<p>Prior to creating the indices, I need to create the index templates. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport requests\nfrom requests.auth import HTTPBasicAuth\nimport json\nfrom time import sleep\n\ndef serialize_sets(obj):\n        if isinstance(obj, set):\n                return list(obj)\n        return obj\n\nlistIgnoredTemplates = &#x5B;&#039;.watch-history&#039;, &#039;.watch-history-1&#039;, &#039;.watch-history-2&#039;, &#039;.watch-history-3&#039;, &#039;.watch-history-4&#039;, &#039;.watch-history-5&#039;, &#039;.watch-history-6&#039;, &#039;.watch-history-7&#039;, &#039;.watch-history-8&#039;, &#039;.watch-history-9&#039;, &#039;.watch-history-10&#039;, &#039;.watch-history-11&#039;, &#039;ilm-history&#039;, &#039;ilm-history_2&#039;, &#039;tenant_template&#039;, &#039;.monitoring-logstash&#039;]\n\n# Get all roles from prod &amp; list users in those roles\nr = requests.get(f&quot;https:\/\/elasticsearch.example.com:9200\/_template&quot;, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n\ndictAllTemplates= r.json()\n\nfor item in dictAllTemplates.items():\n        if item&#x5B;0] not in listIgnoredTemplates:\n                if item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;):\n                        iShards = (item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;).get(&#039;number_of_shards&#039;))\n                        iReplicas = (item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;).get(&#039;number_of_replicas&#039;))\n                else:\n                        iShards = 3\n                        iReplicas = 1\n                if iShards is None:\n                        iShards = 3\n                if iReplicas is None:\n                        iReplicas = 1\n                if item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;) and item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;).get(&#039;lifecycle&#039;):\n                        jsonAddTemplate = {\n                                 &quot;index_patterns&quot;: item&#x5B;1].get(&#039;index_patterns&#039;),\n                                  &quot;template&quot;: {\n                                    &quot;aliases&quot;: {\n                                      item&#x5B;1].get(&#039;settings&#039;).get(&#039;index&#039;).get(&#039;lifecycle&#039;).get(&#039;rollover_alias&#039;): {}\n                                    },\n                                    &quot;settings&quot;: {\n                                      &quot;number_of_shards&quot;: iShards,\n                                      &quot;number_of_replicas&quot;: iReplicas\n                                    },\n                                    &quot;mappings&quot;:        item&#x5B;1].get(&#039;mappings&#039;)\n                                    }\n                                  }\n                else:\n                        jsonAddTemplate = {\n                                  &quot;index_patterns&quot;: item&#x5B;1].get(&#039;index_patterns&#039;),\n                                  &quot;template&quot;: {\n                                    &quot;settings&quot;: {\n                                      &quot;number_of_shards&quot;: iShards,\n                                      &quot;number_of_replicas&quot;: iReplicas\n                                    },\n                                    &quot;mappings&quot;:         item&#x5B;1].get(&#039;mappings&#039;)\n                                    }\n                                  }\n                r2 = requests.put(f&quot;https:\/\/opensearch.example.com:9200\/_index_template\/{item&#x5B;0]}&quot;, json=jsonAddTemplate, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n                print(r2.text)\n                print(r2.status_code)\n                sleep(2)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Prior to creating the indices, I need to create the index templates.<\/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-10860","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\/10860","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=10860"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10860\/revisions"}],"predecessor-version":[{"id":10861,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10860\/revisions\/10861"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}