Gon
Gon

Reputation: 113

How to delete specific document using "id" on Azure Search REST API?

I would like to know how to delete a specific document in an index of Azure Search.

I would like to use "id" to delete a document by using the REST API. I have searched, but couldn't find out the way.

{
    "@odata.context": "https://xxxx/$metadata#docs(*)",
    "value": [
        {
            "@search.score": 1,
            "id": "16",
            "questions": [
                "Question"
            ],
            "answer": "Answer",
            "source": "https://azure.microsoft.com/ja-jp/support/faq/",
            "keywords": [],
            "alternateQuestions": null
 },

For example, I would like to delete only the document whose id is 16. I don't want to delete whole index, just want to delete the document.

If someone knows how to do it, please provide a REST API sample.

Upvotes: 6

Views: 10465

Answers (1)

Carey Halton
Carey Halton

Reputation: 884

The documentation for how to delete "documents" in Azure Search can be found here. Since you want to delete all of the fields associated with id == 16, this should be what you are looking for.

To be more specific for your exact situation, you will want to issue a POST request to the following URI with the appropriate service name, index name and api admin key (as a header) filled in:

POST https://[service name].search.windows.net/indexes/[index name]/docs/index?api-version=2017-11-11  
Content-Type: application/json   
api-key: [admin key]  

And with the following request body:

{  
  "value": [  
    {  
      "@search.action": "delete",  
      "id": "16"  
    }  
  ]  
}

If the request returns 200, then the document will have successfully been deleted from the index.

Note that you can delete more than one document in the same request by including more objects in the JSON array, each with a different "id". This is more efficient than deleting them one at a time.

Upvotes: 16

Related Questions