Elasticsearch-dsl python connection timeout

Hi all,

Fairly new to Elasticsearch and hoping that some of you might be able to help me troubleshoot an attempt to use the python Elasticsearch-dsl to perform a search in a public database?

I am able to access the database with my API-key via either curl or Requests, but I can't get it to work with Elasticsearch-dsl. I imagine I must be forming my query poorly, but despite looking at the documentation I'm not sure what I'm doing wrong.

The following curl call:

curl -v -XPOST -u "user:pass" -H "Content-Type: application/json" \
http://distribution.virk.dk/cvr-permanent/virksomhed/_search -d '
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Vrvirksomhed.cvrNummer": "35128417"
          }
        }
        ]
      }
   },
  "_source":[
    "Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn"
    ]
}' | python -mjson.tool

Yields the correct data (a snippet):

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 6,
        "successful": 6,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 12.333591,
        "hits": [
            {
                "_index": "cvr-v-20200115",
                "_type": "_doc",
                "_id": "4000333365",
                "_score": 12.333591,
                "_source": {
                    "Vrvirksomhed": {
                        "virksomhedMetadata": {
                            "nyesteNavn": {
                                "navn": "RAMB\u00d8LL DANMARK A/S"
                            }
                        }
                    }
                }
            }
        ]
    }
}

But trying to follow the seach example given here to convert that query into Elasticsearch-dsl:

es = Elasticsearch(
    ['http://distribution.virk.dk/cvr-permanent/virksomhed/_search'],
    http_auth=('user', 'password')
)

s = Search(using=es, index="cvr-v-20200115.Vrvirksomhed") \
    .query("match", cvrNummer="35128417")

response = s.execute()

Yields a connection timeout

Traceback (most recent call last):
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 255, in perform_request
    response = self.pool.urlopen(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/util/retry.py", line 386, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/packages/six.py", line 735, in reraise
    raise value
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Users/michh/opt/anaconda3/lib/python3.8/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Users/michh/opt/anaconda3/lib/python3.8/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Users/michh/opt/anaconda3/lib/python3.8/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Users/michh/opt/anaconda3/lib/python3.8/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/Users/michh/opt/anaconda3/lib/python3.8/http/client.py", line 950, in send
    self.connect()
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connection.py", line 187, in connect
    conn = self._new_conn()
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/urllib3/connection.py", line 164, in _new_conn
    raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPConnection object at 0x7fc9b44f2d00>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch_dsl/search.py", line 715, in execute
    self, es.search(index=self._index, body=self.to_dict(), **self._params)
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/client/utils.py", line 347, in _wrapped
    return func(*args, params=params, headers=headers, **kwargs)
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/client/__init__.py", line 1830, in search
    return self.transport.perform_request(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/transport.py", line 417, in perform_request
    self._do_verify_elasticsearch(headers=headers, timeout=timeout)
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/transport.py", line 606, in _do_verify_elasticsearch
    raise error
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/transport.py", line 569, in _do_verify_elasticsearch
    _, info_headers, info_response = conn.perform_request(
  File "/Users/michh/opt/anaconda3/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 280, in perform_request
    raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError((<urllib3.connection.HTTPConnection object at 0x7fc9b44f2d00>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)')) caused by: ConnectTimeoutError((<urllib3.connection.HTTPConnection object at 0x7fc9b44f2d00>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)'))

Thanks in advance for your help!

Is this on the same system? Or any other layer involved like docker containers?

Apologies for the long response time!

All the code is running on the same system, but the API I'm trying to access is hosted elsewhere. No docker or other layers involved, no.

Hey,

try specifying only the Elasticsearch hostname in the client constructor, not the full URL path.

Hi,

It's unclear what the hostname is. How would I determine that - ask the operators, or is there a way I can read it off of the URL? Sorry if this is a stupid question.

Best regards,
MH

Try using only http://distribution.virk.dk instead of the full URL path..

Unfortunately looks like it still throws a connection timeout:

/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 255, in perform_request
    response = self.pool.urlopen(
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/util/retry.py", line 525, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/lib/python3.8/http/client.py", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1011, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 951, in send
    self.connect()
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/home/michh/.local/lib/python3.8/site-packages/urllib3/connection.py", line 179, in _new_conn
    raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPConnection object at 0x7f8133b98280>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "api_connector_3.py", line 14, in <module>
    response = s.execute()
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch_dsl/search.py", line 715, in execute
    self, es.search(index=self._index, body=self.to_dict(), **self._params)
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/client/utils.py", line 347, in _wrapped
    return func(*args, params=params, headers=headers, **kwargs)
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/client/__init__.py", line 1821, in search
    return self.transport.perform_request(
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/transport.py", line 417, in perform_request
    self._do_verify_elasticsearch(headers=headers, timeout=timeout)
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/transport.py", line 606, in _do_verify_elasticsearch
    raise error
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/transport.py", line 569, in _do_verify_elasticsearch
    _, info_headers, info_response = conn.perform_request(
  File "/home/michh/.local/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 280, in perform_request
    raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError((<urllib3.connection.HTTPConnection object at 0x7f8133b98280>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)')) caused by: ConnectTimeoutError((<urllib3.connection.HTTPConnection object at 0x7f8133b98280>, 'Connection to distribution.virk.dk timed out. (connect timeout=10)'))

I guess the best bet is to try asking the people running the server?

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