Is there a way to return true/false if documents exists for the given query?

I have a query:

GET users/_search
{ "from": 0, "size": 20,
"query": {
"match_all": {}
}

What i want to achieve is if the above query returns even a single document i get the response as true and if no document is returned i get false.

How is this possible?

@Hamza_Khalid

I don't understand why do this logic in the query, you can manipulate the query response in your application.
What is the reason for doing this?

Set size to 1 and check whether any matching document was returned.

I want to return a category list of users so for example if my query returns 10 users i want to show those 10 users in a new tab(category list) and this category list should only pop up if the query returns any records else the category list won't be created.

Thank you for the reply but i think you misunderstood the question. whatever i set the size to it will return results doesn't matter what the query is my question is. Is there a way to get response as true if my query return 1 or more document and false if my query returns no document.

For example:

curl -XGET 'http://localhost:9200/twitter/tweet/_search/exists' -d '
{
    "query" : {
    "term" : { "user" : "kimchy" }
    }
}'

Response:

"exists" : true

i found this in different disscussion but this not working for me i'm getting no handler found for uri exists.

No, not that I am aware of, which is why I suggested handling this in your application code.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.