meilisearch / meilisearch-python

Python wrapper for the Meilisearch API
https://meilisearch.com
MIT License
462 stars 85 forks source link

meilisearch.errors.MeiliSearchCommunicationError when try to access indexes #705

Closed charlesdzadu closed 1 year ago

charlesdzadu commented 1 year ago

Description I am a Junior Developer. I am creating an API with FastApi, Mongo, and Meilisearch. I am using docker-compose to link all my containers. This is the first time I try to integrate Meilisearch to my project so I put the configuration in my docker-compose

I try to test connection by doing this import meilisearch client = meilisearch.Client("http://localhost:7700") client.create_index("medias")

Expected behavior I expect to not have error and create successfully this medias index.

Current behavior I have this error

raise MeiliSearchCommunicationError(str(err)) from err
heliumartworks_api-api-1  | meilisearch.errors.MeiliSearchCommunicationError: MeiliSearchCommunicationError, HTTPConnectionPool(host='localhost', port=7700): Max retries exceeded with url: /indexes (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbbff493970>: Failed to establish a new connection: [Errno 111] Connection refused'))

Even if I run meilisearch outside of my project and try to connect I have the same issue. When I try to use Meilisearch Cloud. It works Screenshots or Logs Here is full logs

heliumartworks_api-api-1  | Traceback (most recent call last):
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
heliumartworks_api-api-1  |     conn = connection.create_connection(
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 95, in create_connection
heliumartworks_api-api-1  |     raise err
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 85, in create_connection
heliumartworks_api-api-1  |     sock.connect(sa)
heliumartworks_api-api-1  | ConnectionRefusedError: [Errno 111] Connection refused
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | During handling of the above exception, another exception occurred:
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | Traceback (most recent call last):
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
heliumartworks_api-api-1  |     httplib_response = self._make_request(
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 398, in _make_request
heliumartworks_api-api-1  |     conn.request(method, url, **httplib_request_kw)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 239, in request
heliumartworks_api-api-1  |     super(HTTPConnection, self).request(method, url, body=body, headers=headers)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/http/client.py", line 1285, in request
heliumartworks_api-api-1  |     self._send_request(method, url, body, headers, encode_chunked)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/http/client.py", line 1331, in _send_request
heliumartworks_api-api-1  |     self.endheaders(body, encode_chunked=encode_chunked)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/http/client.py", line 1280, in endheaders
heliumartworks_api-api-1  |     self._send_output(message_body, encode_chunked=encode_chunked)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/http/client.py", line 1040, in _send_output
heliumartworks_api-api-1  |     self.send(msg)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/http/client.py", line 980, in send
heliumartworks_api-api-1  |     self.connect()
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 205, in connect
heliumartworks_api-api-1  |     conn = self._new_conn()
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 186, in _new_conn
heliumartworks_api-api-1  |     raise NewConnectionError(
heliumartworks_api-api-1  | urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fbbff493970>: Failed to establish a new connection: [Errno 111] Connection refused
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | During handling of the above exception, another exception occurred:
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | Traceback (most recent call last):
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 489, in send
heliumartworks_api-api-1  |     resp = conn.urlopen(
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 787, in urlopen
heliumartworks_api-api-1  |     retries = retries.increment(
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
heliumartworks_api-api-1  |     raise MaxRetryError(_pool, url, error or ResponseError(cause))
heliumartworks_api-api-1  | urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=7700): Max retries exceeded with url: /indexes (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbbff493970>: Failed to establish a new connection: [Errno 111] Connection refused'))
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | During handling of the above exception, another exception occurred:
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | Traceback (most recent call last):
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/meilisearch/_httprequests.py", line 44, in send_request
heliumartworks_api-api-1  |     request = http_method(
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 115, in post
heliumartworks_api-api-1  |     return request("post", url, data=data, json=json, **kwargs)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 59, in request
heliumartworks_api-api-1  |     return session.request(method=method, url=url, **kwargs)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
heliumartworks_api-api-1  |     resp = self.send(prep, **send_kwargs)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
heliumartworks_api-api-1  |     r = adapter.send(request, **kwargs)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 565, in send
heliumartworks_api-api-1  |     raise ConnectionError(e, request=request)
heliumartworks_api-api-1  | requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=7700): Max retries exceeded with url: /indexes (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbbff493970>: Failed to establish a new connection: [Errno 111] Connection refused'))
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | The above exception was the direct cause of the following exception:
heliumartworks_api-api-1  | 
heliumartworks_api-api-1  | Traceback (most recent call last):
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
heliumartworks_api-api-1  |     self.run()
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/multiprocessing/process.py", line 108, in run
heliumartworks_api-api-1  |     self._target(*self._args, **self._kwargs)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
heliumartworks_api-api-1  |     target(sockets=sockets)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 60, in run
heliumartworks_api-api-1  |     return asyncio.run(self.serve(sockets=sockets))
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
heliumartworks_api-api-1  |     return loop.run_until_complete(main)
heliumartworks_api-api-1  |   File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
heliumartworks_api-api-1  |     config.load()
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 477, in load
heliumartworks_api-api-1  |     self.loaded_app = import_from_string(self.app)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
heliumartworks_api-api-1  |     module = importlib.import_module(module_str)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
heliumartworks_api-api-1  |     return _bootstrap._gcd_import(name[level:], package, level)
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
heliumartworks_api-api-1  |   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
heliumartworks_api-api-1  |   File "/heliumartworks_api/./app/main.py", line 6, in <module>
heliumartworks_api-api-1  |     from app.search.routers import search
heliumartworks_api-api-1  |   File "/heliumartworks_api/./app/search/routers/search.py", line 2, in <module>
heliumartworks_api-api-1  |     from app.database.meilisearch import client
heliumartworks_api-api-1  |   File "/heliumartworks_api/./app/database/meilisearch.py", line 7, in <module>
heliumartworks_api-api-1  |     client.create_index("medias")
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/meilisearch/client.py", line 67, in create_index
heliumartworks_api-api-1  |     return Index.create(self.config, uid, options)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/meilisearch/index.py", line 142, in create
heliumartworks_api-api-1  |     task = HttpRequests(config).post(config.paths.index, payload)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/meilisearch/_httprequests.py", line 66, in post
heliumartworks_api-api-1  |     return self.send_request(requests.post, path, body, content_type)
heliumartworks_api-api-1  |   File "/usr/local/lib/python3.9/site-packages/meilisearch/_httprequests.py", line 55, in send_request
heliumartworks_api-api-1  |     raise MeiliSearchCommunicationError(str(err)) from err
heliumartworks_api-api-1  | meilisearch.errors.MeiliSearchCommunicationError: MeiliSearchCommunicationError, HTTPConnectionPool(host='localhost', port=7700): Max retries exceeded with url: /indexes (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbbff493970>: Failed to establish a new connection: [Errno 111] Connection refused'))

Environment (please complete the following information):

docker-compose.yml

version: "3.9"

services:

api:

build: .

volumes:

- .:/heliumartworks_api

ports:

- "80:80"

env_file:

- .env

depends_on:

- db

- meilisearch

db:

image: mongo

volumes:

- task_db:/data/db

ports:

- "27017:27017"

meilisearch:

image: getmeili/meilisearch:latest

restart: always

ports:

    - 7700:7700

volumes:

    - meili-search:/data.ms

command:

    - --env=development

    - --http-addr=localhost:7700

volumes:

    task_db:

    meili-search: {}

DockerFile

FROM  python:3.9
WORKDIR  /heliumartworks_api
COPY  ./requirements.txt  /heliumartworks_api/requirements.txt
RUN  pip  install  --no-cache-dir  --upgrade  -r  /heliumartworks_api/requirements.txt
RUN  apt-get  -y  update
RUN  apt-get  install  ffmpeg  libsm6  libxext6  -y
COPY  ./app  /heliumartworks_api/app
CMD  ["uvicorn",  "app.main:app",  "--host",  "0.0.0.0",  "--port",  "80",  "--reload"]

**

sanders41 commented 1 year ago

There are several things that look off to me here. Is your code opens source? If so can you share the repo so I can help you there? If not I'll try to help here.

For the python it should be something like:

from meilisearch import Client

client = Client("http://localhost:7700")
client.create_index("medias")

For the docker compose, 3.8 is the latest version so 3.9 won't work. For meilisearch it will look something like this:

version: "3.8"
services:
  meilisearch:
    image: getmeili/meilisearch:latest
    ports:
      - "7700:7700"
    volumes:
      - data-ms:/data.ms
    environment:
      MEILI_ENV: "development"

volumes:
  data-ms:

Then some suggestions for your Dockerfile:

FROM  python:3.9

WORKDIR  /heliumartworks_api

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  ffmpeg \
  libsm6 \
  libxext6 \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

COPY  ./requirements.txt  /heliumartworks_api/requirements.txt

RUN pip install -U pip && pip --no-cache-dir -r requirements.txt

COPY  ./app  /heliumartworks_api/app

EXPOSE 80

CMD  ["uvicorn",  "app.main:app",  "--host",  "0.0.0.0",  "--port",  "80",  "--reload"]
charlesdzadu commented 1 year ago

Hi @sanders41, thanks for your response.

Is your code opens source? If so can you share the repo so I can help you there?

Yes ! My code is open source, here is a link. Helium Artworks API All update is on dev branch, you can check it.

I update my code with your suggestions, but still get the same issue.

meilisearch.errors.MeiliSearchCommunicationError: MeiliSearchCommunicationError, HTTPConnectionPool(host='localhost', port=7700): Max retries exceeded with url: /indexes (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1af8c6e700>: Failed to establish a new connection: [Errno 111] Connection refused'))

sanders41 commented 1 year ago

Thanks @charlesdzadu. I have looked at this some and since the issues isn't related to the meilisearch sdk I'm going close this issue here, and will come over to your repo to help you there.

charlesdzadu commented 1 year ago

Thanks @sanders41. I am waiting for you on my repo then.

alallema commented 1 year ago

Thank you so much @sanders41 to care of this issue ❤️ Don't hesitate if I can help 😊