Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include `search_type=count` in a query?

I have a Python script that runs many ElasticSearch aggregations, e.g.:

client = Elasticsearch(...)
q =  {"aggs": {"my_name":{"terms": "field", "fieldname"}}}
res = client.search(index = "indexname*", doc_type = "doc_type", body = q)

But this returns the search query (match everything I think) res["hits"] and the aggregation results res["aggregations"].

What I want to run is the Python equivalent of the following

GET /index*/doc_type/_search?search_type=count

{"aggs": {"my_name":{"terms": "field", "fieldname"}}}

How do I make sure to include the ?search_type=count when using Python Elasticsearch?

I'd like to know this in general, but the current reason I'm looking into this is I occasionally get errors caused by timeouts or data size when running the queries. My suspicion is that if I can only ask for the counting then I'll avoid these.

like image 372
travelingbones Avatar asked Nov 20 '15 15:11

travelingbones


1 Answers

The general consensus is to not use search_type=count anymore as it's been deprecated in 2.0. Instead you should simply use size: 0.

res = client.search(index = "indexname*", doc_type = "doc_type", body = q, size=0)
                                                                             ^
                                                                             |
                                                                         add this
like image 169
Val Avatar answered Oct 10 '22 03:10

Val