Check if document exists or not in elasticsearch

Viewed 21078

I want to check if a document with specific field value does exist or not in elasticsearch.

I have gone through internet but only found how to check if a field exists or not.

My Index/Type is

/twitter/user

username is one field in document.

I want to check if username="xyz" exists or not in this Type.

4 Answers

From the documentation:

If all you want to do is to check whether a document exists—you’re not interested in the content at all—then use the HEAD method instead of the GET method. HEAD requests don’t return a body, just HTTP headers:

curl -i -XHEAD http://localhost:9200/twitter/user/userid

Elasticsearch will return a 200 OK status code if the document exists ... and a 404 Not Found if it doesn’t exist

Note: userid is the value of the _id field.

Related