简体   繁体   English

使用 python 的 Elasticsearch package 连接到远程 Elasticsearch 服务器

[英]Connecting to remote Elasticsearch server with python's Elasticsearch package

I want to use a remote Elasticsearch server for my website.我想为我的网站使用远程 Elasticsearch 服务器。

I have used elastic.co/ cloud service to create a remote Elasticsearch server.我已经使用elastic.co/服务创建了一个远程 Elasticsearch 服务器。 I can connect/ping remote Elasticsearch server using the following command (it is scrubbed of sensitive info): curl -u username:password https://55555555555bb0c30d1cba4e9e6.us-central1.gcp.cloud.es.io:9243 I can connect/ping remote Elasticsearch server using the following command (it is scrubbed of sensitive info): curl -u username:password https://55555555555bb0c30d1cba4e9e6.us-central1.gcp.cloud.es.io:9243

After tying this command into terminal, I receive the following response:将此命令绑定到终端后,我收到以下响应:

{
  "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"
}

I am using the following Elasticsearch python client to integrate Elasticsearch with my website: https://elasticsearch-py.readthedocs.io/en/v7.10.1/index.html# I am using the following Elasticsearch python client to integrate Elasticsearch with my website: https://elasticsearch-py.readthedocs.io/en/v7.10.1/index.html#

To initialize a local connection, I import the Elasticsearch class and initialize an instance of that class by pasting in the local url of my Elasticsearch server. To initialize a local connection, I import the Elasticsearch class and initialize an instance of that class by pasting in the local url of my Elasticsearch server.

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

Now I want to connect to my remote server using the same Elasticsearch class.现在我想使用相同的 Elasticsearch class 连接到我的远程服务器。 To do this, I need to format the initialization of the Elasticsearch object with the info to connect to my remote elasticsearch server.为此,我需要使用连接到我的远程 elasticsearch 服务器的信息来格式化 Elasticsearch object 的初始化。

This is where I am having some trouble.这是我遇到麻烦的地方。 The docstring for the Elasticsearch class is very opaque. Elasticsearch class 的文档字符串非常不透明。 In short, it is asking me to create a custom connection object and I have not been able to figure it out how to do it.简而言之,它要求我创建一个自定义连接 object 并且我无法弄清楚如何去做。

The documentation for the Elasticsearch object is a [little bit better][1] but still does not give an example involving a username and password. Elasticsearch object 的文档[稍微好一点][1] 但仍然没有给出涉及用户名和密码的示例。

I have the ability to create an API key, but I am not sure how to use it.我有能力创建一个 API 密钥,但我不知道如何使用它。 An answer involving either an API key or answering how to connect using my username and password, would be very helpful.涉及 API 密钥或回答如何使用我的用户名和密码进行连接的答案将非常有帮助。

You need to connect using TLS/SSL and Authentication as described in the documentation.您需要按照文档中的说明使用TLS/SSL 和身份验证进行连接。

In your case you should use something like this.在你的情况下,你应该使用这样的东西。

from elasticsearch import Elasticsearch

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM