Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch return total hits only

i want to send query to ELS that return only the total hits.

without anything else

like if i get the respone

{   "took": 1111,   "timed_out": false,   "_shards": {
    "total": 9,
    "successful": 9,
    "failed": 0   },   "hits": {
    "total": 731552,   
 }

i want to print only 731552

for now i just send :

curl http://server:9200/games_profilder/_search

thanks

like image 667
art20170 Avatar asked May 03 '17 11:05

art20170


People also ask

How do I get total hits in Elasticsearch?

The track_total_hits parameter allows you to control how the total number of hits should be tracked. Given that it is often enough to have a lower bound of the number of hits, such as "there are at least 10000 hits", the default is set to 10,000 .

How do I get more than 10 results in Elasticsearch?

If a search request results in more than ten hits, ElasticSearch will, by default, only return the first ten hits. To override that default value in order to retrieve more or fewer hits, we can add a size parameter to the search request body.

How do I get Elasticsearch to index all data?

You can use cURL in a UNIX terminal or Windows command prompt, the Kibana Console UI, or any one of the various low-level clients available to make an API call to get all of the documents in an Elasticsearch index. All of these methods use a variation of the GET request to search the index.


1 Answers

You can use response filtering for this:

curl http://server:9200/games_profilder/_search?filter_path=hits.total

which will yield

{
  "hits": {
    "total": 731552
  }
}

If you really want to only get the total, you can pipe the result with jq like this:

curl http://server:9200/games_profilder/_search?filter_path=hits.total | jq '.hits.total'

and that will yield only the number 731552

like image 143
Val Avatar answered Oct 13 '22 13:10

Val