elastic/elasticsearch-py

feature request - quit esql query by interrupt windows powershell

Closed this issue · 3 comments

I find it hard to work on large indexes, seems like I'm unable to interrupt a query once it has fired.

But I'm unable to get <ctrl+c> or any interrupt to work:


class Esql:
    def __init__(self, env:str='test', timeout:int=60, timefield:str='time'):
        self.client = elasticsearch.Elasticsearch(hosts[env], request_timeout=timeout)
:
        try:
            response = self.client.esql.query(
                query=query, format=format
            )
        except (elasticsearch.exceptions.BadRequestError) as e:
            log.error(e)
            return None
        except (KeyboardInterrupt, SystemExit):
            log.error("interrupted")
            sys.exit(-1)
        except Exception as e:
            log.error(f"unmapped exception {e}")
            return None


20250121132640.032|INFO|C:\dist\elk\tools\esql.py:468|query: FROM bps-trace-* | WHERE @timestamp >= "2025-01-21T11:25:37.727Z" AND @timestamp <= "2025-01-21T12:25:37.728Z" | WHERE bps.application=="id-pro-trk-trk-fido2-service-test12" | KEEP `message`
20250121132713.958|ERROR|C:\dist\elk\tools\esql.py:284|interrupted

I get KeyboardInterrupt only after the query has returned after 23sec

And setting the request_timeout will exit if it reached the value.

A way to quit it at anytime would be nice.

Hello, I'm unable to reproduce. Using this snippet:

import os
from elasticsearch import Elasticsearch

client = Elasticsearch(
    "http://localhost:9200", api_key=os.environ["ES_LOCAL_API_KEY"], request_timeout=60
)
print(client.info())
print("esql")
client.esql.query(query="FROM kibana_sample_data_ecommerce")

I then ran Elasticsearch with start-local and loaded the sample ecommerce data in Kibana. I then Ctrl-C as soon as I see "esql" and get a KeyboardInterrupt exception as expected, while the client was waiting for the response:

$ source elastic-start-local/.env
$ ES_LOCAL_API_KEY=$ES_LOCAL_API_KEY python issues/2758.py 
{'name': '2ff212ac86f6', 'cluster_name': 'docker-cluster', 'cluster_uuid': 'gjECK54RSpm4NKOs5Vf5ng', 'version': {'number': '8.17.0', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '2b6a7fed44faa321997703718f07ee0420804b41', 'build_date': '2024-12-11T12:08:05.663969764Z', 'build_snapshot': False, 'lucene_version': '9.12.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}
esql
^CTraceback (most recent call last):
  File ".../issues/2758.py", line 9, in <module>
    client.esql.query(query="FROM kibana_sample_data_ecommerce")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../elasticsearch/_sync/client/utils.py", line 455, in wrapped
    return api(*args, **kwargs)
  File ".../elasticsearch/_sync/client/esql.py", line 401, in query
    return self.perform_request(  # type: ignore[return-value]
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        "POST",
        ^^^^^^^
    ...<5 lines>...
        path_parts=__path_parts,
        ^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../elasticsearch/_sync/client/_base.py", line 423, in perform_request
    return self._client.perform_request(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        method,
        ^^^^^^^
    ...<5 lines>...
        path_parts=path_parts,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
        method,
    ...<4 lines>...
        otel_span=otel_span,
    )
  File ".../elasticsearch/_sync/client/_base.py", line 316, in _perform_request
    meta, resp_body = self.transport.perform_request(
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        method,
        ^^^^^^^
    ...<8 lines>...
        otel_span=otel_span,
        ^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../.venv/lib/python3.13/site-packages/elastic_transport/_transport.py", line 342, in perform_request
    resp = node.perform_request(
        method,
    ...<3 lines>...
        request_timeout=request_timeout,
    )
  File ".../.venv/lib/python3.13/site-packages/elastic_transport/_node/_http_urllib3.py", line 167, in perform_request
    response = self.pool.urlopen(
        method,
    ...<4 lines>...
        **kw,  # type: ignore[arg-type]
    )
  File ".../.venv/lib/python3.13/site-packages/urllib3/connectionpool.py", line 789, in urlopen
    response = self._make_request(
        conn,
    ...<10 lines>...
        **response_kw,
    )
  File ".../.venv/lib/python3.13/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File ".../.venv/lib/python3.13/site-packages/urllib3/connection.py", line 507, in getresponse
    httplib_response = super().getresponse()
  File ".../lib/python3.13/http/client.py", line 1452, in getresponse
    response.begin()
    ~~~~~~~~~~~~~~^^
  File ".../lib/python3.13/http/client.py", line 355, in begin
    version, status, reason = self._read_status()
                              ~~~~~~~~~~~~~~~~~^^
  File ".../lib/python3.13/http/client.py", line 316, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
               ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File ".../lib/python3.13/socket.py", line 743, in readinto
    return self._sock.recv_into(b)
           ~~~~~~~~~~~~~~~~~~~~^^^
KeyboardInterrupt

Tank for this feedback, I tested now inside WSL and there it works just great. But I'm unable to get it to work in windows, I believe I have a plain powershell shell

Ah, that makes sense, thank you. This isn't specific to this library, though, but is a more general issue with Powershell and Python: https://stackoverflow.com/questions/42039231/ctrl-c-for-quitting-python-in-powershell-now-not-working