尝试在Python中连接到ElasticSearch时出现连接错误,但可以使用相同的凭据通过Postman连接

vq8itlhq  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(56)

我可以使用clean index和Bearer token的URI在Postman中查询ES。我有以下代码来测试从Python查询:

from elasticsearch import Elasticsearch
...
token = response['access_token']
url = 'https://cleanindex.staging.xyz.com/' 
es = Elasticsearch(
    hosts=[{'host': url, 'port': 9200, 'scheme':'https'}],
    basic_auth=('bearer_auth', token),
    request_timeout=10,
    verify_certs=False
)

字符串
当我尝试执行查询测试时,我得到一个连接错误:

# Perform a query
index = 'staging_index/_search'
query = {
    'query': {
        'match': {
            'field_name': 'search_term',
        },
    }
}
try:
    results = es.search(index=index, body=query,timeout=5)
except Exception as e:
    print(e)


连接错误原因:ConnectionError(连接错误原因:NameResolutionError(<urllib3.connection.HTTPSConnection object at 0x000002102BCD4A60>:未能解析“https://cleanindex.staging.xyz.com/”([Errno 11001] getaddrinfo failed)
如何解决此错误?
附加错误:
x1c 0d1x的数据

mbyulnm0

mbyulnm01#

您应该按照文档中的描述进行操作,不指定hosts参数,而只是将完整的URL作为第一个参数:

token = response['access_token']
url = 'https://cleanindex.staging.xyz.com:9200/'
es = Elasticsearch(
    url,
    bearer_auth=token,
    request_timeout=10,
    verify_certs=False
)

字符串

相关问题