【问题标题】:Connecting to remote Elasticsearch server with python's Elasticsearch package使用 python 的 Elasticsearch 包连接到远程 Elasticsearch 服务器
【发布时间】:2021-04-25 03:14:09
【问题描述】:

我想为我的网站使用远程 Elasticsearch 服务器。

我已经使用elastic.co/ 云服务创建了一个远程 Elasticsearch 服务器。 我可以使用以下命令连接/ping 远程 Elasticsearch 服务器(它已清除敏感信息): curl -u username:password https://55555555555bb0c30d1cba4e9e6.us-central1.gcp.cloud.es.io:9243

将此命令绑定到终端后,我收到以下响应:

{
  "name" : "instance-0000000001",
  "cluster_name" : "555555555555",
  "cluster_uuid" : "55555555555",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "555555555555555555555555",
    "build_date" : "2021-01-13T00:42:12.435326Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

我正在使用以下 Elasticsearch python 客户端将 Elasticsearch 与我的网站集成:https://elasticsearch-py.readthedocs.io/en/v7.10.1/index.html#

为了初始化本地连接,我导入 Elasticsearch 类并通过粘贴到我的 Elasticsearch 服务器的本地 url 中初始化该类的实例。

>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch('http://localhost:9200')
>>> es
<Elasticsearch([{'host': 'localhost', 'port': 9200}])>

现在我想使用相同的 Elasticsearch 类连接到我的远程服务器。为此,我需要使用连接到远程 Elasticsearch 服务器的信息来格式化 Elasticsearch 对象的初始化。

这是我遇到麻烦的地方。 Elasticsearch 类的文档字符串非常不透明。简而言之,它要求我创建一个自定义连接对象,但我无法弄清楚如何去做。

Elasticsearch 对象的文档[稍微好一点][1] 但仍然没有给出涉及用户名和密码的示例。

我有能力创建一个 API 密钥,但我不知道如何使用它。涉及 API 密钥或回答如何使用我的用户名和密码进行连接的答案将非常有帮助。

【问题讨论】:

    标签: python elasticsearch curl


    【解决方案1】:

    您需要按照文档中的说明使用TLS/SSL and Authentication 进行连接。

    在你的情况下,你应该使用这样的东西。

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch(
        ['5555555555bb0c30d1cba4e9e6.us-central1.gcp.cloud.es.io'],
        http_auth=('username', 'password'),
        scheme="https",
        port=9243,
    )
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 2016-03-09
      • 2019-09-27
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2020-04-03
      相关资源
      最近更新 更多