ValueError: URL must include a 'scheme', 'host', and 'port' component
Asked Answered
P

3

10

Trying to use elastic search for a project.

from elasticsearch import Elasticsearch
es = Elasticsearch(
    "https://example.com",
    http_auth=("abc", "bcd"),
)

But getting the error:

ValueError: URL must include a 'scheme', 'host', and 'port' component (ie 'https://localhost:9200')

I am running this on Pycharm CE and have created a Virtual Environment of Python 3.9 . Should I be using a different version of Python to make this work?? Moreover, I have already tried pip install acryl-datahub[datahub-rest,elasticsearch]==0.8.27.1 but to no avail.

Panada answered 27/8, 2022 at 16:12 Comment(0)
L
8

I was facing the same error. Installing ES version 7.17 solved the problem. I was also getting a not available on pip install acryl-datahub[datahub-rest,elasticsearch]==0.8.27.1. Tried with the next one available (0.8.28.0) and realized it was downgrading elasticsearch to 7.17.

So try pip install elasticsearch==7.17.

Luciusluck answered 14/9, 2022 at 10:37 Comment(0)
R
0

Using these lines of code in settings.py helped me to avoid this error.

ELK_BASE_URL = 'elasticsearch://{username}:{password}@{host_ip}:{host_port}'
ELASTIC_SEARCH_URL = ELK_BASE_URL.format(
    username='ELASTICSEARCH_USER',
    password='ELASTICSEARCH_PASS',
    host_ip='elasticsearch', # 'elasticsearch' - service name in docker-compose.yml
    host_port='9200'
)
    
ELASTICSEARCH_DSL = {
    'default': {
        'hosts': [ELASTIC_SEARCH_URL]
    },
}

Or you can use just:

ELASTICSEARCH_DSL = {
        'default': {
            'hosts': 'http://elasticsearch:9200' # same as above
        },
}

p.s. you can also try it with 'localhost' instead of 'elasticsearch'.

Revere answered 30/10, 2023 at 8:24 Comment(0)
W
0

To solve the problem write this instead:

ELASTICSEARCH_DSL={
    'default': {
        'hosts': 'https://localhost:9200',
        'http_auth': ('username', 'password')
    }
}
Wyn answered 11/9 at 8:42 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Favela

© 2022 - 2024 — McMap. All rights reserved.