0

When I do a query Elasticsearch returns how many hits I get. Can I also get it to reply how many documents it has in total? Here I've added the imaginary field sum_documents to the result. Does such thing exist, or to I have to make an extra query to fetch the sum?

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "sum_documents": 500,
    "max_score" : null,
    "hits" : [ ]
  }
}

1 Answer 1

1

You can add a global aggregation in your query, and it will return the total document count in your search context (index/alias + type(s))

{
  "query": {
    "query_string": {
      "query": "viking",
      "default_operator": "AND"
    }
  },
  "aggs": {
    "harvester-test": {
      "global": {}
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Val. Thanks for the suggestion. I could not get it working, though. Would you mind having a look? I've updated the post with the query I tried.
I've updated my answer. aggs should be at the top level, i.e. at the same level as the query part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.