Issues connecting to App Search hosted on Elasticsearch service through Python API

I'm trying to index a document on my App Search Instance running on Elasticsearch Service, using the python API.

My code is as follows

    from elastic_app_search import Client
    client = Client(
        base_endpoint='https://<redacted>.app-search.eastus2.azure.elastic-cloud.com/api/as/v1/',
        api_key='private-<redacted>',
        use_https=True
    )

    engine_name = '<redacted>'

    def indexDoc(doc):
        return client.index_document(engine_name, doc)

    def main():
        doc = {
            "title": "Test",
            "text": "This worked. Woohoo!"
        }
        print(indexDoc(doc))

    if __name__ == '__main__':
        main()

This yielded the following error:


    Traceback (most recent call last):
      File "/Users/hari/.pyenv/versions/3.8.2/lib/python3.8/site-packages/urllib3/connection.py", line 159, in _new_conn
        conn = connection.create_connection(
      File "/Users/hari/.pyenv/versions/3.8.2/lib/python3.8/site-packages/urllib3/util/connection.py", line 61, in create_connection
        for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
      File "/Users/hari/.pyenv/versions/3.8.2/lib/python3.8/socket.py", line 918, in getaddrinfo
        for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno 8] nodename nor servname provided, or not known

This error is printed several times, followed by:

    requests.exceptions.ConnectionError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: //<redacted>/app-search.eastus2.azure.elastic-cloud.com/api/as/v1//engines/<redacted engine-name>/documents (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10a64c400>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))

I tried a non https version of this code:

    client = Client(
    base_endpoint='4e9d28aa6be4481499db518c9f094532.app-search.eastus2.azure.elastic-cloud.com/api/as/v1/',
    api_key='private-ztxmrx3ppvk17pezdkn8iien',
    use_https=False
)

Running this, I get multiple errors which look like:

    requests.exceptions.ConnectionError: HTTPConnectionPool(host='<redacted>.app-search.eastus2.azure.elastic-cloud.com', port=80): Max retries exceeded with url: /api/as/v1//engines/<redacted engine name>/documents (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10e6b72b0>: Failed to establish a new connection: [Errno 60] Operation timed out'))

I would ideally like to connect to App Search in HTTPS and index my document. How do I do this?

Make sure you exclude the protocol from "base_endpoint".


That fixed my problem!

I did try excluding the protocol in the past, but I would also set use_https=false.

The key is to exclude protocol and set use_https=true

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.