TypeError: __init__() missing 1 required positional argument: 'scheme' in Elastic Search

Viewed 2983

Below is my code-

Elasticsearch is not using https protocol, it's using http protocol.

pip uninstall elasticsearch
pip install elasticsearch==7.13.4
import elasticsearch.helpers
from elasticsearch import Elasticsearch
# from elasticsearch import Elasticsearch, RequestsHttpConnection

es_host = '<>'
es_port = '<>'
es_username = '<>'
es_password = '><'
es_index = '<>'

es = Elasticsearch([{'host':str(es_host),'port':str(es_port)}], http_auth=(str(es_username), str(es_password)))

es.indices.refresh(index=es_index)

Error-

10 es = Elasticsearch([{'host': str(es_host), 'port': str(es_port)}],http_auth=(str(es_username), str(es_password)))
     11 
     12 es.indices.refresh(index=es_index)

3 frames
/usr/local/lib/python3.7/dist-packages/elasticsearch/_sync/client/__init__.py in __init__(self, hosts, cloud_id, api_key, basic_auth, bearer_auth, opaque_id, headers, connections_per_node, http_compress, verify_certs, ca_certs, client_cert, client_key, ssl_assert_hostname, ssl_assert_fingerprint, ssl_version, ssl_context, ssl_show_warn, transport_class, request_timeout, node_class, node_pool_class, randomize_nodes_in_pool, node_selector_class, dead_node_backoff_factor, max_dead_node_backoff, serializer, serializers, default_mimetype, max_retries, retry_on_status, retry_on_timeout, sniff_on_start, sniff_before_requests, sniff_on_node_failure, sniff_timeout, min_delay_between_sniffing, sniffed_node_callback, meta_header, timeout, randomize_hosts, host_info_callback, sniffer_timeout, sniff_on_connection_fail, http_auth, maxsize, _transport)

/usr/local/lib/python3.7/dist-packages/elasticsearch/_sync/client/utils.py in client_node_configs(hosts, cloud_id, **kwargs)

/usr/local/lib/python3.7/dist-packages/elasticsearch/_sync/client/utils.py in hosts_to_node_configs(hosts)

/usr/local/lib/python3.7/dist-packages/elasticsearch/_sync/client/utils.py in host_mapping_to_node_config(host)

TypeError: __init__() missing 1 required positional argument: 'scheme'

When I add "scheme"

Code-

es = Elasticsearch([{'host':str(es_host),'port':str(es_port)}], http_auth=(str(es_username), str(es_password)), scheme="http",verify_certs=False)

Error-

__init__() got an unexpected keyword argument 'scheme'

I checked and tried connection to ES but its not connecting.

3 Answers

According to Elasticsearch python library, for different versions of Elasticsearch installed on your machine, you must have a compatible python Elasticsearch library. For example, in my case, I had Elasticsearch version 7.17.3 installed, and I had to install the same version from PyPI which can be installed with the following command:

pip install elasticsearch==7.17.3

Also, you can check the versions of python Elasticsearch library available by looking at this page.

I ran into a similar error. I am using elasticsearch==8.3.1. When you construct your url with the list of dictionaries, you need to define the schema. Add "scheme": "https" to your dictionary and that will solve the missing argument.

es = Elasticsearch(
    [
        {'host': 'localhost', 'port': '9200', "scheme": "https"}
    ],
        basic_auth=('elastic', '<password>')
)

In your case, you should convert your instantiation to as follows:

es = Elasticsearch(
    [
        {
            'host':str(es_host),
            'port':str(es_port),
            'scheme': "https"
        }
    ], 
    http_auth=(str(es_username), str(es_password))
)

I am not sure if the scheme is http or https, that is something you'll need to dig into.

What's the version of Elasticsearch you were trying to connect to?

I had the same error that came from trying to connect to Elasticsearch v7 with the python client v8.

Related