Files
doc-exports/docs/css/umn/css_01_0043.html
zhengxiu 93d856d5c5 css umn 25.6.0 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: zhengxiu <zhengxiu@huawei.com>
Co-committed-by: zhengxiu <zhengxiu@huawei.com>
2025-11-25 11:34:43 +00:00

436 lines
44 KiB
HTML

<a name="EN-US_TOPIC_0000001992165573"></a><a name="EN-US_TOPIC_0000001992165573"></a>
<h1 class="topictitle1">Accessing an OpenSearch Cluster with the Python Client</h1>
<div id="body0000001992165573"><p id="EN-US_TOPIC_0000001992165573__p16884844152514">You can access and manage data in a CSS OpenSearch cluster using the official OpenSearch Python client (opensearch-py) or Elasticsearch Python client (elasticsearch-py). Both client libraries are compatible with OpenSearch REST APIs, though version compatibility needs to be verified for elasticsearch-py. Developers can use Python APIs to manage indexes; create, read, update, and delete (CRUD) documents; and search data.</p>
<p id="EN-US_TOPIC_0000001992165573__p11884154418253">You are advised to use the OpenSearch Python client (opensearch-py) to access complete OpenSearch features. For details, see <a href="https://docs.opensearch.org/latest/clients/python-low-level/" target="_blank" rel="noopener noreferrer">OpenSearch Python Client Documentation</a> or <a href="https://www.elastic.co/docs/reference/elasticsearch/clients/python" target="_blank" rel="noopener noreferrer">Elasticsearch Python Client Documentation</a>.</p>
<div class="section" id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_section4509421174413"><h4 class="sectiontitle">Prerequisites</h4><ul id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_ul115524012184"><li id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_li181191829144220">The target OpenSearch cluster is available.</li><li id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_li578832518434">The server that runs the Python code can communicate with the OpenSearch cluster.</li><li id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001945377010_li578832518434">Depending on the network configuration method used, obtain the cluster access address. For details, see <a href="css_01_0289.html#EN-US_TOPIC_0000001992165561__en-us_topic_0000001975823337_section855085010198">Network Configuration</a>.</li><li id="EN-US_TOPIC_0000001992165573__li58808201592">Python 3 has been installed on the server.</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section15525202285618"><h4 class="sectiontitle">Installing the Python Client Library</h4><p id="EN-US_TOPIC_0000001992165573__p845163813184">Install the Python client library on the server that runs the Python code.</p>
<p id="EN-US_TOPIC_0000001992165573__p118151312191612">CSS allows you to use the Elasticsearch 7.10 Python client to connect to OpenSearch clusters. To ensure better compatibility, however, you are advised to use a Python client library that has the same version as the target OpenSearch cluster.</p>
<p id="EN-US_TOPIC_0000001992165573__p198383566448">Select a reference example based on the Python client you use.</p>
<ul id="EN-US_TOPIC_0000001992165573__ul48397565220"><li id="EN-US_TOPIC_0000001992165573__li6771329142613">Scenario 1 (recommended): Use the OpenSearch Python client to access an OpenSearch cluster and install the OpenSearch Python client library.<pre class="screen" id="EN-US_TOPIC_0000001992165573__screen53361555182311">pip install opensearch-py</pre>
</li><li id="EN-US_TOPIC_0000001992165573__li159005485252">Scenario 2: Use the Elasticsearch 7.10 Python client to access an OpenSearch cluster and install the Elasticsearch 7.10 Python client library.<pre class="screen" id="EN-US_TOPIC_0000001992165573__screen13108134192416">pip install Elasticsearch==7.10</pre>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section14782152316147"><h4 class="sectiontitle">Cluster Access Scenarios</h4><p id="EN-US_TOPIC_0000001992165573__p147091993211">The sample code varies depending on the Python client and the security mode settings of the target OpenSearch cluster. Select the right reference document based on your service scenario.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001992165573__table8908141516224" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Cluster access scenarios</caption><thead align="left"><tr id="EN-US_TOPIC_0000001992165573__row89091315182211"><th align="left" class="cellrowborder" valign="top" width="21.122112211221122%" id="mcps1.3.5.3.2.4.1.1"><p id="EN-US_TOPIC_0000001992165573__p55191237114110">Python Client Type</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="32.39323932393239%" id="mcps1.3.5.3.2.4.1.2"><p id="EN-US_TOPIC_0000001992165573__p1190919153229">OpenSearch Cluster Security-Mode Settings</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="46.48464846484649%" id="mcps1.3.5.3.2.4.1.3"><p id="EN-US_TOPIC_0000001992165573__p17909151512211">Reference Document</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001992165573__row14909915102214"><td class="cellrowborder" rowspan="3" valign="top" width="21.122112211221122%" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p218104254116">OpenSearch</p>
</td>
<td class="cellrowborder" valign="top" width="32.39323932393239%" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1190901518224">Non-security mode</p>
</td>
<td class="cellrowborder" valign="top" width="46.48464846484649%" headers="mcps1.3.5.3.2.4.1.3 "><p id="EN-US_TOPIC_0000001992165573__p915152716415"><a href="#EN-US_TOPIC_0000001992165573__section12544103941817">Connecting to a Non-Security Mode Cluster with the OpenSearch Python Client</a></p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row1290971532215"><td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p9478715303">Security mode + HTTP</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1859429552"><a href="#EN-US_TOPIC_0000001992165573__section5657637173211">Connecting to a Security-Mode+HTTP Cluster with the OpenSearch Python Client</a></p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row20764174992411"><td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p106042320307">Security mode + HTTPS</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p12174325612"><a href="#EN-US_TOPIC_0000001992165573__section819316143350">Connecting to a Security-Mode+HTTPS Cluster with the OpenSearch Python Client</a></p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row9114143123615"><td class="cellrowborder" rowspan="3" valign="top" width="21.122112211221122%" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p65341249174119">Elasticsearch 7.10.2</p>
</td>
<td class="cellrowborder" valign="top" width="32.39323932393239%" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1882714584516">Non-security mode</p>
</td>
<td class="cellrowborder" valign="top" width="46.48464846484649%" headers="mcps1.3.5.3.2.4.1.3 "><p id="EN-US_TOPIC_0000001992165573__p33791452717"><a href="#EN-US_TOPIC_0000001992165573__section64071057163511">Connecting to a Non-Security Mode Cluster with the Elasticsearch Python Client</a></p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row54722383614"><td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p11827453457">Security mode + HTTP</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p53791152720"><a href="#EN-US_TOPIC_0000001992165573__section47791925143714">Connecting to a Security-Mode+HTTP Cluster with the Elasticsearch Python Client</a></p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row396781117367"><td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.1 "><p id="EN-US_TOPIC_0000001992165573__p1682810594510">Security mode + HTTPS</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.5.3.2.4.1.2 "><p id="EN-US_TOPIC_0000001992165573__p2037935372"><a href="#EN-US_TOPIC_0000001992165573__section984185711374">Connecting to a Security-Mode+HTTPS Cluster with the Elasticsearch Python Client</a></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section12544103941817"><a name="EN-US_TOPIC_0000001992165573__section12544103941817"></a><a name="section12544103941817"></a><h4 class="sectiontitle">Connecting to a Non-Security Mode Cluster with the OpenSearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p5423105723016">Use the OpenSearch Python client to connect to an OpenSearch cluster for which the security mode is disabled, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b363893118238">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p1568204272614">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<div class="codecoloring" codetype="Python" id="EN-US_TOPIC_0000001992165573__screen874765141812"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span></pre></div></td><td class="code"><div><pre><span></span><span class="kn">from</span> <span class="nn">opensearchpy</span> <span class="kn">import</span> <span class="n">OpenSearch</span>
<span class="k">class</span> <span class="nc">OpenSearchFactory</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">username</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">password</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="n">port</span>
<span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
<span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>
<span class="bp">self</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="n">password</span>
<span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">OpenSearch</span><span class="p">:</span>
<span class="n">addrs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">host</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="p">:</span>
<span class="n">addr</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'host'</span><span class="p">:</span> <span class="n">host</span><span class="p">,</span> <span class="s1">'port'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span><span class="p">}</span>
<span class="n">addrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">,</span> <span class="n">http_auth</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">opensearch</span>
<span class="n">os</span> <span class="o">=</span> <span class="n">OpenSearchFactory</span><span class="p">([</span><span class="s2">&quot;{cluster address}&quot;</span><span class="p">],</span> <span class="s2">&quot;9200&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">create</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">indices</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="s1">'test'</span><span class="p">))</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001992165573__p23421347192818">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b108509501351">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue48500507518"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue198503501353"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section5657637173211"><a name="EN-US_TOPIC_0000001992165573__section5657637173211"></a><a name="section5657637173211"></a><h4 class="sectiontitle">Connecting to a Security-Mode+HTTP Cluster with the OpenSearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p765763763211">Use the OpenSearch Python client to connect to a security-mode OpenSearch cluster that uses HTTP, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b1492214461296">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p17657137123212">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<div class="codecoloring" codetype="Python" id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_screen3453924204619"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span></pre></div></td><td class="code"><div><pre><span></span><span class="kn">from</span> <span class="nn">opensearchpy</span> <span class="kn">import</span> <span class="n">OpenSearch</span>
<span class="k">class</span> <span class="nc">OpenSearchFactory</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">username</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">password</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="n">port</span>
<span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
<span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>
<span class="bp">self</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="n">password</span>
<span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">OpenSearch</span><span class="p">:</span>
<span class="n">addrs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">host</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="p">:</span>
<span class="n">addr</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'host'</span><span class="p">:</span> <span class="n">host</span><span class="p">,</span> <span class="s1">'port'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span><span class="p">}</span>
<span class="n">addrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">,</span> <span class="n">http_auth</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">opensearch</span>
<span class="n">os</span> <span class="o">=</span> <span class="n">OpenSearchFactory</span><span class="p">([</span><span class="s2">&quot;xxx.xxx.xxx.xxx&quot;</span><span class="p">],</span> <span class="s2">&quot;9200&quot;</span><span class="p">,</span> <span class="s2">&quot;username&quot;</span><span class="p">,</span> <span class="s2">&quot;password&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">create</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">indices</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="s1">'test'</span><span class="p">))</span>
</pre></div></td></tr></table></div>
</div>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001992165573__table1564712275347" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Variables</caption><thead align="left"><tr id="EN-US_TOPIC_0000001992165573__row364882793410"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.7.5.2.3.1.1"><p id="EN-US_TOPIC_0000001992165573__p2064842711346">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.7.5.2.3.1.2"><p id="EN-US_TOPIC_0000001992165573__p1664814271344">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001992165573__row66481927193412"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p064852714347">host</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p364802753410">IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row66481127143411"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p36481027193419">port</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p16648227123417">Access port of the cluster. Enter <strong id="EN-US_TOPIC_0000001992165573__b67571809392836">9200</strong>.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row564882711342"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p11648162719346">username</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p2648142703416">Username for accessing the cluster.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row1648027193413"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p166481227103417">password</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.7.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p864815277344">Password of the user.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001992165573__p965853783216">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b1526019281364">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue22611128463"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue22615284611"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section819316143350"><a name="EN-US_TOPIC_0000001992165573__section819316143350"></a><a name="section819316143350"></a><h4 class="sectiontitle">Connecting to a Security-Mode+HTTPS Cluster with the OpenSearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p619331412357">Use the OpenSearch Python client to connect to a security-mode OpenSearch cluster that uses HTTPS, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b1310474264317">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p219316145356">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<div class="codecoloring" codetype="Python" id="EN-US_TOPIC_0000001992165573__en-us_topic_0000001972375893_en-us_topic_0000001961178817_screen76972122304"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span></pre></div></td><td class="code"><div><pre><span></span><span class="kn">from</span> <span class="nn">opensearchpy</span> <span class="kn">import</span> <span class="n">OpenSearch</span>
<span class="kn">import</span> <span class="nn">ssl</span>
<span class="k">class</span> <span class="nc">OpenSearchFactory</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">username</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">password</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="n">port</span>
<span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
<span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>
<span class="bp">self</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="n">password</span>
<span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">OpenSearch</span><span class="p">:</span>
<span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">_create_unverified_context</span><span class="p">()</span>
<span class="n">addrs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">host</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="p">:</span>
<span class="n">addr</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'host'</span><span class="p">:</span> <span class="n">host</span><span class="p">,</span> <span class="s1">'port'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span><span class="p">}</span>
<span class="n">addrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">,</span> <span class="n">http_auth</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span><span class="p">),</span> <span class="n">scheme</span><span class="o">=</span><span class="s2">&quot;https&quot;</span><span class="p">,</span> <span class="n">ssl_context</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">opensearch</span> <span class="o">=</span> <span class="n">OpenSearch</span><span class="p">(</span><span class="n">addrs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">opensearch</span>
<span class="n">os</span> <span class="o">=</span> <span class="n">OpenSearchFactory</span><span class="p">([</span><span class="s2">&quot;xxx.xxx.xxx.xxx&quot;</span><span class="p">],</span> <span class="s2">&quot;9200&quot;</span><span class="p">,</span> <span class="s2">&quot;username&quot;</span><span class="p">,</span> <span class="s2">&quot;password&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">create</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">indices</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="s1">'test'</span><span class="p">))</span>
</pre></div></td></tr></table></div>
</div>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001992165573__table119316141351" frame="border" border="1" rules="all"><caption><b>Table 3 </b>Variables</caption><thead align="left"><tr id="EN-US_TOPIC_0000001992165573__row13194111419351"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.8.5.2.3.1.1"><p id="EN-US_TOPIC_0000001992165573__p71941114183515">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.8.5.2.3.1.2"><p id="EN-US_TOPIC_0000001992165573__p14194131453514">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001992165573__row14194814103518"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p91941814103519">host</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1919441443516">IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row119431418359"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p519481413516">port</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1419491416351">Access port of the cluster. Enter <strong id="EN-US_TOPIC_0000001992165573__b13015520444">9200</strong>.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row171940146356"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p13194201493515">username</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p6194101410353">Username for accessing the cluster.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row18194191413358"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p419461415354">password</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.8.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p219417149351">Password of the user.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001992165573__p919471463515">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b175567301263">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue1855663012615"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue2556123020619"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section64071057163511"><a name="EN-US_TOPIC_0000001992165573__section64071057163511"></a><a name="section64071057163511"></a><h4 class="sectiontitle">Connecting to a Non-Security Mode Cluster with the Elasticsearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p64078577356">Use the Elasticsearch 7.10.2 Python client to connect to an OpenSearch cluster for which the security mode is disabled, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b17588454917">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p17407757153510">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<pre class="screen" id="EN-US_TOPIC_0000001992165573__screen08471351195817">from elasticsearch import Elasticsearch
class ElasticFactory(object):
def __init__(self, host: list, port: str, username: str, password: str):
self.port = port
self.host = host
self.username = username
self.password = password
def create(self) -&gt; Elasticsearch:
addrs = []
for host in self.host:
addr = {'host': host, 'port': self.port}
addrs.append(addr)
if self.username and self.password:
elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
else:
elasticsearch = Elasticsearch(addrs)
return elasticsearch
es = ElasticFactory(["{cluster address}"], "9200", None, None).create()
print(es.indices.exists(index='test'))</pre>
<p id="EN-US_TOPIC_0000001992165573__p14407195793510">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b19371832865">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue5371732264"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue173713328611"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section47791925143714"><a name="EN-US_TOPIC_0000001992165573__section47791925143714"></a><a name="section47791925143714"></a><h4 class="sectiontitle">Connecting to a Security-Mode+HTTP Cluster with the Elasticsearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p13780112519370">Use the Elasticsearch 7.10.2 Python client to connect to a security-mode OpenSearch cluster that uses HTTP, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b320581712500">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p16780132511375">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<pre class="screen" id="EN-US_TOPIC_0000001992165573__screen1457492075918">from elasticsearch import Elasticsearch
class ElasticFactory(object):
def __init__(self, host: list, port: str, username: str, password: str):
self.port = port
self.host = host
self.username = username
self.password = password
def create(self) -&gt; Elasticsearch:
addrs = []
for host in self.host:
addr = {'host': host, 'port': self.port}
addrs.append(addr)
if self.username and self.password:
elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
else:
elasticsearch = Elasticsearch(addrs)
return elasticsearch
es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(es.indices.exists(index='test'))</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001992165573__table4780132510371" frame="border" border="1" rules="all"><caption><b>Table 4 </b>Variables</caption><thead align="left"><tr id="EN-US_TOPIC_0000001992165573__row27816258378"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.10.5.2.3.1.1"><p id="EN-US_TOPIC_0000001992165573__p15781225183717">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.10.5.2.3.1.2"><p id="EN-US_TOPIC_0000001992165573__p27811025143717">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001992165573__row147818252377"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p1178112516371">host</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p127814256378">IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row177811025143711"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p2078114258373">port</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p157818252377">Access port of the cluster. Enter <strong id="EN-US_TOPIC_0000001992165573__b0730244185120">9200</strong>.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row117815251372"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p117814258379">username</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p6781725153715">Username for accessing the cluster.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row478116259371"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p6781112513717">password</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.10.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p778272516373">Password of the user.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001992165573__p19782172543717">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b1143411343616">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue2043513411620"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue1943516341669"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165573__section984185711374"><a name="EN-US_TOPIC_0000001992165573__section984185711374"></a><a name="section984185711374"></a><h4 class="sectiontitle">Connecting to a Security-Mode+HTTPS Cluster with the Elasticsearch Python Client</h4><p id="EN-US_TOPIC_0000001992165573__p5841357123717">Use the Elasticsearch 7.10.2 Python client to connect to a security-mode OpenSearch cluster that uses HTTPS, and query whether the <strong id="EN-US_TOPIC_0000001992165573__b134914945211">test</strong> index exists.</p>
<p id="EN-US_TOPIC_0000001992165573__p6841195719374">The following is an example of the code for creating a client instance using the cluster connection information:</p>
<pre class="screen" id="EN-US_TOPIC_0000001992165573__screen322105014596">from elasticsearch import Elasticsearch
import ssl
class ElasticFactory(object):
def __init__(self, host: list, port: str, username: str, password: str):
self.port = port
self.host = host
self.username = username
self.password = password
def create(self) -&gt; Elasticsearch:
context = ssl._create_unverified_context()
addrs = []
for host in self.host:
addr = {'host': host, 'port': self.port}
addrs.append(addr)
if self.username and self.password:
elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password), scheme="https", ssl_context=context)
else:
elasticsearch = Elasticsearch(addrs)
return elasticsearch
es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(es.indices.exists(index='test'))</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001992165573__table7842125783712" frame="border" border="1" rules="all"><caption><b>Table 5 </b>Variables</caption><thead align="left"><tr id="EN-US_TOPIC_0000001992165573__row28433579378"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.11.5.2.3.1.1"><p id="EN-US_TOPIC_0000001992165573__p884395793710">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.11.5.2.3.1.2"><p id="EN-US_TOPIC_0000001992165573__p118431579378">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001992165573__row128434578379"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p384345723716">host</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p884315710377">IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row158439578373"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p1843185773713">port</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1684335714372">Access port of the cluster. Enter <strong id="EN-US_TOPIC_0000001992165573__b1587618358529">9200</strong>.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row1084311577374"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p1984385720379">username</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p1984495716379">Username for accessing the cluster.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001992165573__row128441057143713"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.1 "><p id="EN-US_TOPIC_0000001992165573__p08449578375">password</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.11.5.2.3.1.2 "><p id="EN-US_TOPIC_0000001992165573__p684412573373">Password of the user.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001992165573__p58441579376">This piece of code checks whether the <strong id="EN-US_TOPIC_0000001992165573__b62349361362">test</strong> index exists in the cluster. If <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue162358361460"><b>true</b></span> (the index exists) or <span class="parmvalue" id="EN-US_TOPIC_0000001992165573__parmvalue823573619618"><b>false</b></span> (the index does not exist) is returned, it indicates that the cluster is connected.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="css_01_0288.html">Accessing an OpenSearch Cluster</a></div>
</div>
</div>