Elasticsearch-py: Define timeout for a "count"

Created on 9 Sep 2014  路  7Comments  路  Source: elastic/elasticsearch-py

It seems like it is possible to pass a timeout argument to a search:

 es.search(
    index=my_index,
    doc_type='actions',
    timeout = '900',
    body=
    {
        "size": 0,
        "query": {
            "constant_score": {
                "filter": {
                    "terms": {
                        "ID":  IDs
                    }
                },
                "boost": 1.2
            }
        }
    })

It is then possible to count the number of documents by looking at the ['hits']['total']. A more direct approach would be to use the Elasticsearch.count. However, when doing so it is not possible to pass a timeout argument. A workaround is to use a search with the search_type set to count.

Doesn't it make sense to have also for the count the possibility to set a timeout?

Most helpful comment

global parameter timeout sets the timeout on the socket, per-request parameter request_timeout does the same. The reason it's named differently is because in some cases the APIs already have a parameter named timeout that does something else (internal timeout within elasticsearch).

I tried to document this in the docs: http://elasticsearch-py.readthedocs.org/en/master/api.html#timeout

please let me know if that's confusing or what could be clearer there, thanks!

All 7 comments

This is unfortunately not up to us - elasticsearch itself doesn't support timeout parameter for the count API as it does for search. You can, however, use the request_timeout parameter that is available with every API and it's probably what you want.

timeout is a parameter exposed by elasticsearch that controls internal timeouts, it doesn't guarantee that the request will terminate by that time. request_timeout on the other hand works by setting a timeout on the connection itself, thus making sure your application will never wait any longer.

Does this make sense?

Thanks

Thanks for the quick reply! It does make sense. I have two clarification follow up questions:

  1. request_timeout has to be set when I set the instance of the client, right?
  2. Having in mind a search: Is setting this global parameter equivalent to setting the timeout option to the search itself?

Cheers,
Dror

global parameter timeout sets the timeout on the socket, per-request parameter request_timeout does the same. The reason it's named differently is because in some cases the APIs already have a parameter named timeout that does something else (internal timeout within elasticsearch).

I tried to document this in the docs: http://elasticsearch-py.readthedocs.org/en/master/api.html#timeout

please let me know if that's confusing or what could be clearer there, thanks!

Some API calls also accept a timeout parameter

refers to, for instance, the search API?

One more point, I probably missed it, but it would be nice to mention the default values of the parameters in question.

The default is 10 seconds for the global timeout (mentioned in [0]) and if request_timeout is not specified the global timeout will be used.

Yes, the search API is an example of an API that allows for the timeout parameter that will just be passed to elasticsearch.

0 - http://elasticsearch-py.readthedocs.org/en/master/connection.html#connection

Well then, seems like this is it for now :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tlinhart picture tlinhart  路  5Comments

peta15 picture peta15  路  6Comments

RitterHou picture RitterHou  路  3Comments

alanbacon picture alanbacon  路  3Comments

tirkarthi picture tirkarthi  路  6Comments