ElasticSearch count returned result
Asked Answered
S

3

10

I want to count number of document returned as a result of a query with size limit. For example, I run following query:

curl -XGET http://localhost:9200/logs_-*/a_logs/_search?pretty=true -d '
{
"query" : {
    "match_all" : {  }
},
"size" : 5,
"from" : 8318
}'

and I get:

{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 159,
"successful" : 159,
"failed" : 0
},
  "hits" : {
  "total" : 8319,
  "max_score" : 1.0,
  "hits" : [ {
....

Total documents matching my query are 8319, but I fetched at max 5. Only 1 document was returned since I queried "from" 8318.

In the response, I do not know how many documents are returned. I want to write a query such that the number of documents being returned are also present in some field. Maybe some facet may help, but I could not figure out. Kindly help.

Snuff answered 15/5, 2013 at 12:26 Comment(3)
Can't you just count the number of objects in the hits array on the client side?Alible
That is an expensive operation. I want to know the number before I start counting/processing individual objects.Snuff
There's no way to get that information back. The idea is that if you already parsed the json response it shouldn't be a big problem to read the length of that array, but if you have a big json object and you read it using a pull parser, then it's a completely different story.Alible
S
9

Your query :

{
"query" : {
    "match_all" : {  }
},

=> Means that you ask all your data

"size" : 5,

=> You want to display only 5 results

"from" : 8318

=> You start from the 8318 records

ElasticSearch respons :

....

"hits" : {
  "total" : 8319,

... => Elastic search told you that there is 8319 results in his index.

You ask him all the result and you start from the 8318.

8319 - 8318 = 1 So you have 1 result.

Try by removing the from.

Shorthorn answered 6/11, 2013 at 7:2 Comment(0)
S
1

Looking through the documentation, it's not clear how to make the query return this -- if indeed the API supports it. If you just want to have the count of the returned hits, the easiest way seems to be to actually count them yourself after parsing the response.

Spraggins answered 15/5, 2013 at 13:42 Comment(0)
B
0

Use GET /{index}/_count to count all(fast) or use GET /{index}/_search with size=0 and track_total_hits: true to get all counts.

Use _search:

{
  "query": {
            "exists": {
                "field": "foo"
            }
    },
    "size": 0,
    "track_total_hits":true
}
Beore answered 3/5 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.