如果只想检查一个文档是否存在——根本不想关心内容——那么用HEAD方法来代替GET 方法。HEAD请求没有返回体,只返回一个HTTP请求报头:

    1. curl -i -XHEAD http://localhost:9200/website/blog/123

    如果文档存在,ELasticsearch将返回一个200 OK的状态码:

    HTTP/1.1 200 OK
    Content-Type: text/plain; charset=UTF-8
    Content-Length: 0
    

    若文档不存在,ELasticsearch将返回一个 404 Not Found的状态码:

    curl -i -XHEAD http://localhost:9200/website/blog/124
    
    HTTP/1.1 404 Not Found
    Content-Type: text/plain; charset=UTF-8
    Content-Length: 0
    

    当然,一个文档仅仅是在检查的时候不存在,并不意味着一毫秒之后他也不存在:也许同时正好另一个进程就创建了该文档。