I would just use the get api which returns a 404 if the object doesn't exist, otherwise the object itself. If you use the Java API you'll find an isExists
method in the GetResponse
object.
If the _id
field you are referring to is not included in your documents, saying fields=_id
wouldn't give you back either the _source
nor any specific field under fields
. But you would get back the _id
in the header of the response anyway.
If you are using the REST api you can use the following:
curl -XHEAD 'http://localhost:9200/twitter/tweet/1
it won't return the document back but just 404 if not found, 200 otherwise. The body of the response will also contain the exists flag too, with the same meaning.
What's interesting is that using the HEAD method maps to a get request internally, that's why it's not directly exposed to the Java API, but you can obtain the same behaviour creating a GetRequest
with the following code:
GetRequest getRequest = new GetRequest("index", "type", "id");
// don't get any fields back...
getRequest.fields(new String[0]);