{"id":1158382,"date":"2025-01-13T18:40:02","date_gmt":"2025-01-13T10:40:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1158382.html"},"modified":"2025-01-13T18:40:05","modified_gmt":"2025-01-13T10:40:05","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5es%e8%ae%a4%e8%af%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158382.html","title":{"rendered":"Python\u5982\u4f55\u8fde\u63a5es\u8ba4\u8bc1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200333\/6d297002-9387-4600-ab2e-bc53b228ee26.webp\" alt=\"Python\u5982\u4f55\u8fde\u63a5es\u8ba4\u8bc1\" \/><\/p>\n<p><p> <strong>Python\u8fde\u63a5Elasticsearch\u8ba4\u8bc1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Elasticsearch\u5ba2\u6237\u7aef\u5e93\u3001\u914d\u7f6e\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\u3001\u914d\u7f6eAPI\u5bc6\u94a5\u8ba4\u8bc1\u4ee5\u53ca\u4f7f\u7528OAuth 2.0\u8ba4\u8bc1\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Elasticsearch\u5ba2\u6237\u7aef\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b98\u65b9Elasticsearch Python\u5ba2\u6237\u7aef\u5e93\uff08Elasticsearch-py\uff09\u8fde\u63a5Elasticsearch\u96c6\u7fa4\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u914d\u7f6e\u8fde\u63a5\u548c\u8ba4\u8bc1\u4fe1\u606f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Elasticsearch-py\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install elasticsearch<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6765\u8fdb\u884c\u8fde\u63a5\u548c\u8ba4\u8bc1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u4f7f\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\u8fde\u63a5Elasticsearch<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    http_auth=(&#39;user&#39;, &#39;password&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>http_auth<\/code>\u53c2\u6570\u6765\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u6307\u5b9a\u4e86\u96c6\u7fa4\u7684URL\u548c\u7aef\u53e3\u53f7\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u57fa\u672c\u8ba4\u8bc1<\/h2>\n<\/p>\n<p><p>\u57fa\u672c\u8ba4\u8bc1\u662f\u6700\u7b80\u5355\u7684\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728HTTP\u8bf7\u6c42\u5934\u4e2d\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u5b9e\u73b0\u3002\u5728Elasticsearch-py\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>http_auth<\/code>\u53c2\u6570\u6765\u914d\u7f6e\u57fa\u672c\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u4f7f\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\u8fde\u63a5Elasticsearch<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    http_auth=(&#39;user&#39;, &#39;password&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>http_auth<\/code>\u53c2\u6570\u63a5\u6536\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542b\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u8fd9\u6837\uff0c\u5728\u6bcf\u6b21\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5ba2\u6237\u7aef\u4f1a\u81ea\u52a8\u5c06\u8fd9\u4e9b\u8ba4\u8bc1\u4fe1\u606f\u6dfb\u52a0\u5230\u8bf7\u6c42\u5934\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001API\u5bc6\u94a5\u8ba4\u8bc1<\/h2>\n<\/p>\n<p><p>Elasticsearch\u652f\u6301\u4f7f\u7528API\u5bc6\u94a5\u8fdb\u884c\u8ba4\u8bc1\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u57fa\u672c\u8ba4\u8bc1\u66f4\u5b89\u5168\u3002\u6211\u4eec\u9700\u8981\u9996\u5148\u751f\u6210\u4e00\u4e2aAPI\u5bc6\u94a5\uff0c\u7136\u540e\u5728\u5ba2\u6237\u7aef\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u5bc6\u94a5\u8fdb\u884c\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728Elasticsearch\u4e2d\u521b\u5efaAPI\u5bc6\u94a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">curl -X POST &quot;https:\/\/localhost:9200\/_security\/api_key&quot; -u user:password -H &quot;Content-Type: application\/json&quot; -d&#39;<\/p>\n<p>{<\/p>\n<p>  &quot;name&quot;: &quot;my-api-key&quot;,<\/p>\n<p>  &quot;role_descriptors&quot;: {<\/p>\n<p>    &quot;my-api-key-role&quot;: {<\/p>\n<p>      &quot;cluster&quot;: [&quot;all&quot;],<\/p>\n<p>      &quot;index&quot;: [<\/p>\n<p>        {<\/p>\n<p>          &quot;names&quot;: [&quot;*&quot;],<\/p>\n<p>          &quot;privileges&quot;: [&quot;all&quot;]<\/p>\n<p>        }<\/p>\n<p>      ]<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my-api-key<\/code>\u7684API\u5bc6\u94a5\uff0c\u5e76\u6388\u4e88\u5b83\u5bf9\u6240\u6709\u7d22\u5f15\u7684\u6240\u6709\u6743\u9650\u3002\u8fd4\u56de\u7684JSON\u54cd\u5e94\u4e2d\u5305\u542b\u4e86API\u5bc6\u94a5\u7684ID\u548c\u5bc6\u94a5\u503c\u3002<\/p>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u5ba2\u6237\u7aef\u4e2d\u4f7f\u7528API\u5bc6\u94a5\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u4f7f\u7528API\u5bc6\u94a5\u8ba4\u8bc1\u8fde\u63a5Elasticsearch<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    api_key=(&#39;api_key_id&#39;, &#39;api_key_value&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>api_key<\/code>\u53c2\u6570\u63a5\u6536\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542bAPI\u5bc6\u94a5\u7684ID\u548c\u5bc6\u94a5\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001OAuth 2.0\u8ba4\u8bc1<\/h2>\n<\/p>\n<p><p>OAuth 2.0\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8ba4\u8bc1\u6388\u6743\u65b9\u5f0f\uff0cElasticsearch\u4e5f\u652f\u6301\u8fd9\u79cd\u65b9\u5f0f\u3002\u6211\u4eec\u9700\u8981\u9996\u5148\u83b7\u53d6\u4e00\u4e2a\u8bbf\u95ee\u4ee4\u724c\uff0c\u7136\u540e\u5728\u5ba2\u6237\u7aef\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u4ee4\u724c\u8fdb\u884c\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u6709\u4e00\u4e2a\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c\u7684API\u7aef\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c<\/strong><\/h2>\n<p>response = requests.post(&#39;https:\/\/auth.example.com\/oauth\/token&#39;, data={<\/p>\n<p>    &#39;grant_type&#39;: &#39;password&#39;,<\/p>\n<p>    &#39;client_id&#39;: &#39;client_id&#39;,<\/p>\n<p>    &#39;client_secret&#39;: &#39;client_secret&#39;,<\/p>\n<p>    &#39;username&#39;: &#39;user&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;password&#39;<\/p>\n<p>})<\/p>\n<p>token = response.json()[&#39;access_token&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u5ba2\u6237\u7aef\u4e2d\u4f7f\u7528\u8bbf\u95ee\u4ee4\u724c\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u4f7f\u7528OAuth 2.0\u4ee4\u724c\u8ba4\u8bc1\u8fde\u63a5Elasticsearch<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    headers={&#39;Authorization&#39;: f&#39;Bearer {token}&#39;},<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>headers<\/code>\u53c2\u6570\u5c06\u8bbf\u95ee\u4ee4\u724c\u6dfb\u52a0\u5230HTTP\u8bf7\u6c42\u5934\u4e2d\uff0c\u4ee5\u8fdb\u884cOAuth 2.0\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001SSL\/TLS\u914d\u7f6e<\/h2>\n<\/p>\n<p><p>\u5728\u8fde\u63a5Elasticsearch\u65f6\uff0c\u542f\u7528SSL\/TLS\u52a0\u5bc6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u88ab\u7a83\u542c\u6216\u7be1\u6539\u3002\u5728Elasticsearch-py\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6eSSL\/TLS\u53c2\u6570\u6765\u542f\u7528\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Elasticsearch\u96c6\u7fa4\u4e0a\u914d\u7f6eSSL\/TLS\u3002\u5177\u4f53\u6b65\u9aa4\u53ef\u4ee5\u53c2\u8003Elasticsearch\u5b98\u65b9\u6587\u6863\u3002\u7136\u540e\uff0c\u5728Python\u5ba2\u6237\u7aef\u4e2d\u914d\u7f6eSSL\/TLS\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<h2><strong>\u914d\u7f6eSSL\/TLS\u8fde\u63a5<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    http_auth=(&#39;user&#39;, &#39;password&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>    ssl_context={<\/p>\n<p>        &#39;verify_certs&#39;: True,<\/p>\n<p>        &#39;ca_certs&#39;: &#39;\/path\/to\/ca.crt&#39;,<\/p>\n<p>        &#39;client_cert&#39;: &#39;\/path\/to\/client.crt&#39;,<\/p>\n<p>        &#39;client_key&#39;: &#39;\/path\/to\/client.key&#39;<\/p>\n<p>    }<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>ssl_context<\/code>\u53c2\u6570\u63a5\u6536\u4e00\u4e2a\u5305\u542bSSL\/TLS\u914d\u7f6e\u7684\u5b57\u5178\u3002\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u662f\u5426\u9a8c\u8bc1\u8bc1\u4e66\u3001CA\u8bc1\u4e66\u8def\u5f84\u3001\u5ba2\u6237\u7aef\u8bc1\u4e66\u548c\u5ba2\u6237\u7aef\u5bc6\u94a5\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u9ad8\u7ea7\u914d\u7f6e<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8fde\u63a5\u548c\u8ba4\u8bc1\u914d\u7f6e\u5916\uff0cElasticsearch-py\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u914d\u7f6e\u9009\u9879\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u63a7\u5236\u8fde\u63a5\u884c\u4e3a\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u8fde\u63a5\u6c60\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>Elasticsearch-py\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406HTTP\u8fde\u63a5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u8fde\u63a5\u6c60\u53c2\u6570\u6765\u4f18\u5316\u8fde\u63a5\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<p>from elasticsearch.connection import RequestsHttpConnection<\/p>\n<h2><strong>\u914d\u7f6e\u8fde\u63a5\u6c60<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    http_auth=(&#39;user&#39;, &#39;password&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>    connection_class=RequestsHttpConnection,<\/p>\n<p>    maxsize=10,  # \u6700\u5927\u8fde\u63a5\u6570<\/p>\n<p>    retry_on_timeout=True,  # \u8d85\u65f6\u91cd\u8bd5<\/p>\n<p>    timeout=30  # \u8bf7\u6c42\u8d85\u65f6<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>maxsize<\/code>\u53c2\u6570\u914d\u7f6e\u6700\u5927\u8fde\u63a5\u6570\uff0c<code>retry_on_timeout<\/code>\u53c2\u6570\u914d\u7f6e\u662f\u5426\u5728\u8d85\u65f6\u540e\u91cd\u8bd5\uff0c<code>timeout<\/code>\u53c2\u6570\u914d\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>2. \u91cd\u8bd5\u7b56\u7565<\/h3>\n<\/p>\n<p><p>Elasticsearch-py\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u91cd\u8bd5\u7b56\u7565\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8bf7\u6c42\u5931\u8d25\u65f6\u81ea\u52a8\u91cd\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<p>from elasticsearch.transport import Transport<\/p>\n<h2><strong>\u914d\u7f6e\u91cd\u8bd5\u7b56\u7565<\/strong><\/h2>\n<p>es = Elasticsearch(<\/p>\n<p>    [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>    http_auth=(&#39;user&#39;, &#39;password&#39;),<\/p>\n<p>    scheme=&quot;https&quot;,<\/p>\n<p>    port=9200,<\/p>\n<p>    transport_class=Transport,<\/p>\n<p>    retry_on_status=[500, 502, 503, 504],  # \u91cd\u8bd5\u7684HTTP\u72b6\u6001\u7801<\/p>\n<p>    max_retries=3  # \u6700\u5927\u91cd\u8bd5\u6b21\u6570<\/p>\n<p>)<\/p>\n<h2><strong>\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if es.ping():<\/p>\n<p>    print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>retry_on_status<\/code>\u53c2\u6570\u914d\u7f6e\u9700\u8981\u91cd\u8bd5\u7684HTTP\u72b6\u6001\u7801\uff0c<code>max_retries<\/code>\u53c2\u6570\u914d\u7f6e\u6700\u5927\u91cd\u8bd5\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u8ba4\u8bc1\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u8ba4\u8bc1\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8ba4\u8bc1\u9519\u8bef\u53ca\u5176\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>1. \u8ba4\u8bc1\u5931\u8d25<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0cElasticsearch\u4f1a\u8fd4\u56de401 Unauthorized\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u9519\u8bef\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<p>from elasticsearch.exceptions import AuthenticationException<\/p>\n<p>try:<\/p>\n<p>    es = Elasticsearch(<\/p>\n<p>        [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>        http_auth=(&#39;user&#39;, &#39;wrong_password&#39;),<\/p>\n<p>        scheme=&quot;https&quot;,<\/p>\n<p>        port=9200,<\/p>\n<p>    )<\/p>\n<p>    if es.ping():<\/p>\n<p>        print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Connection failed&quot;)<\/p>\n<p>except AuthenticationException as e:<\/p>\n<p>    print(&quot;Authentication failed:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. API\u5bc6\u94a5\u65e0\u6548<\/h3>\n<\/p>\n<p><p>\u5982\u679cAPI\u5bc6\u94a5\u65e0\u6548\uff0cElasticsearch\u4f1a\u8fd4\u56de403 Forbidden\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u9519\u8bef\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<p>from elasticsearch.exceptions import AuthorizationException<\/p>\n<p>try:<\/p>\n<p>    es = Elasticsearch(<\/p>\n<p>        [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>        api_key=(&#39;invalid_api_key_id&#39;, &#39;invalid_api_key_value&#39;),<\/p>\n<p>        scheme=&quot;https&quot;,<\/p>\n<p>        port=9200,<\/p>\n<p>    )<\/p>\n<p>    if es.ping():<\/p>\n<p>        print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Connection failed&quot;)<\/p>\n<p>except AuthorizationException as e:<\/p>\n<p>    print(&quot;Authorization failed:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4ee4\u724c\u8fc7\u671f<\/h3>\n<\/p>\n<p><p>\u5982\u679cOAuth 2.0\u8bbf\u95ee\u4ee4\u724c\u8fc7\u671f\uff0cElasticsearch\u4f1a\u8fd4\u56de401 Unauthorized\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u9519\u8bef\u5e76\u5237\u65b0\u4ee4\u724c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from elasticsearch import Elasticsearch<\/p>\n<p>from elasticsearch.exceptions import AuthenticationException<\/p>\n<p>import requests<\/p>\n<p>def get_access_token():<\/p>\n<p>    response = requests.post(&#39;https:\/\/auth.example.com\/oauth\/token&#39;, data={<\/p>\n<p>        &#39;grant_type&#39;: &#39;password&#39;,<\/p>\n<p>        &#39;client_id&#39;: &#39;client_id&#39;,<\/p>\n<p>        &#39;client_secret&#39;: &#39;client_secret&#39;,<\/p>\n<p>        &#39;username&#39;: &#39;user&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;password&#39;<\/p>\n<p>    })<\/p>\n<p>    return response.json()[&#39;access_token&#39;]<\/p>\n<p>try:<\/p>\n<p>    token = get_access_token()<\/p>\n<p>    es = Elasticsearch(<\/p>\n<p>        [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>        headers={&#39;Authorization&#39;: f&#39;Bearer {token}&#39;},<\/p>\n<p>        scheme=&quot;https&quot;,<\/p>\n<p>        port=9200,<\/p>\n<p>    )<\/p>\n<p>    if es.ping():<\/p>\n<p>        print(&quot;Connected to Elasticsearch&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Connection failed&quot;)<\/p>\n<p>except AuthenticationException as e:<\/p>\n<p>    print(&quot;Authentication failed:&quot;, e)<\/p>\n<p>    # \u5237\u65b0\u4ee4\u724c<\/p>\n<p>    token = get_access_token()<\/p>\n<p>    es = Elasticsearch(<\/p>\n<p>        [&#39;https:\/\/localhost:9200&#39;],<\/p>\n<p>        headers={&#39;Authorization&#39;: f&#39;Bearer {token}&#39;},<\/p>\n<p>        scheme=&quot;https&quot;,<\/p>\n<p>        port=9200,<\/p>\n<p>    )<\/p>\n<p>    if es.ping():<\/p>\n<p>        print(&quot;Connected to Elasticsearch with new token&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Connection failed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>get_access_token<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u65b0\u7684\u8bbf\u95ee\u4ee4\u724c\uff0c\u5e76\u5728\u6355\u83b7\u5230\u8ba4\u8bc1\u9519\u8bef\u540e\u5237\u65b0\u4ee4\u724c\u5e76\u91cd\u8bd5\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u8fde\u63a5Elasticsearch\u8ba4\u8bc1\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u57fa\u672c\u8ba4\u8bc1\u3001API\u5bc6\u94a5\u8ba4\u8bc1\u3001OAuth 2.0\u8ba4\u8bc1\u4ee5\u53caSSL\/TLS\u914d\u7f6e\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u914d\u7f6e\u9009\u9879\uff0c\u5982\u8fde\u63a5\u6c60\u914d\u7f6e\u548c\u91cd\u8bd5\u7b56\u7565\uff0c\u5e76\u8ba8\u8bba\u4e86\u5982\u4f55\u5904\u7406\u5e38\u89c1\u7684\u8ba4\u8bc1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u914d\u7f6e\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5728\u8fde\u63a5Elasticsearch\u96c6\u7fa4\u65f6\u5177\u6709\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u60a8\u5728\u4f7f\u7528Python\u8fde\u63a5Elasticsearch\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6eElasticsearch\u7684\u8fde\u63a5\u8ba4\u8bc1\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fde\u63a5Elasticsearch\u65f6\uff0c\u60a8\u9700\u8981\u4f7f\u7528Elasticsearch\u5e93\u7684<code>Elasticsearch<\/code>\u7c7b\uff0c\u5e76\u5728\u8fde\u63a5\u65f6\u63d0\u4f9b\u8ba4\u8bc1\u4fe1\u606f\u3002\u8fd9\u901a\u5e38\u5305\u62ec\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from elasticsearch import Elasticsearch\n\nes = Elasticsearch(\n    [&#39;http:\/\/localhost:9200&#39;],\n    http_auth=(&#39;username&#39;, &#39;password&#39;)\n)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>username<\/code>\u548c<code>password<\/code>\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u8ba4\u8bc1\u4fe1\u606f\u3002\u6b64\u4ee3\u7801\u7247\u6bb5\u4f1a\u521b\u5efa\u4e00\u4e2a\u4e0eElasticsearch\u670d\u52a1\u5668\u7684\u8fde\u63a5\uff0c\u5e76\u4f7f\u7528\u63d0\u4f9b\u7684\u51ed\u8bc1\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u8fde\u63a5Elasticsearch\u65f6\uff0c\u5982\u4f55\u5904\u7406SSL\u8bc1\u4e66\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u7684Elasticsearch\u670d\u52a1\u5668\u4f7f\u7528\u4e86SSL\u8bc1\u4e66\uff0c\u60a8\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9a<code>use_ssl=True<\/code>\u548c<code>verify_certs=True<\/code>\u3002\u8fd9\u4f1a\u786e\u4fdd\u8fde\u63a5\u7684\u5b89\u5168\u6027\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u81ea\u7b7e\u540d\u8bc1\u4e66\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u63d0\u4f9b\u8bc1\u4e66\u6587\u4ef6\u7684\u8def\u5f84\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">es = Elasticsearch(\n    [&#39;https:\/\/localhost:9200&#39;],\n    http_auth=(&#39;username&#39;, &#39;password&#39;),\n    use_ssl=True,\n    verify_certs=False  # \u5bf9\u4e8e\u81ea\u7b7e\u540d\u8bc1\u4e66\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e3aFalse\n)\n<\/code><\/pre>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5efa\u8bae\u5c06<code>verify_certs<\/code>\u8bbe\u7f6e\u4e3aTrue\uff0c\u5e76\u63d0\u4f9b\u6709\u6548\u7684\u8bc1\u4e66\u4ee5\u786e\u4fdd\u5b89\u5168\u8fde\u63a5\u3002<\/p>\n<p><strong>\u5982\u679c\u8fde\u63a5Elasticsearch\u65f6\u9047\u5230\u8ba4\u8bc1\u5931\u8d25\uff0c\u5e94\u8be5\u5982\u4f55\u6392\u67e5\u95ee\u9898\uff1f<\/strong><br \/>\u8ba4\u8bc1\u5931\u8d25\u901a\u5e38\u662f\u7531\u4e8e\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0c\u6216\u8005\u7528\u6237\u6ca1\u6709\u8db3\u591f\u7684\u6743\u9650\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u8f93\u5165\u7684\u51ed\u8bc1\u662f\u6b63\u786e\u7684\u3002\u5176\u6b21\uff0c\u68c0\u67e5\u7528\u6237\u662f\u5426\u5177\u5907\u8bbf\u95eeElasticsearch\u7684\u6743\u9650\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7Elasticsearch\u7684\u89d2\u8272\u7ba1\u7406\u529f\u80fd\u6765\u67e5\u770b\u7528\u6237\u6743\u9650\u3002\u6b64\u5916\uff0c\u67e5\u770bElasticsearch\u7684\u65e5\u5fd7\u6587\u4ef6\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u6709\u7528\u7684\u4fe1\u606f\uff0c\u5e2e\u52a9\u60a8\u627e\u5230\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fde\u63a5Elasticsearch\u8ba4\u8bc1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Elasticsearch\u5ba2\u6237\u7aef\u5e93\u3001\u914d\u7f6e\u57fa\u672c\u8eab\u4efd\u9a8c [&hellip;]","protected":false},"author":3,"featured_media":1158390,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158382"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1158382"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158382\/revisions"}],"predecessor-version":[{"id":1158391,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158382\/revisions\/1158391"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158390"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}