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

1

6

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.

Ambler answered 24/1, 2019 at 4:26 Comment(5)
Do you mean delete a field, or delete a document?Cist
@BruceJohnston Each "id" is associate with "questions", "answer" and "source" etc, right? What I want to do is, if I designate id (for example: 16), I would like to delete only the block of 16. I am sorry that I am not sure the block is called "field" or "document". I thought the block is called "field".Ambler
I don't know what you mean by "block" in this situation either. In Azure Search, the JSON object with a key field (in your case I'm assuming it's "id") is called a "document", while the properties of the object ("id", "questions", "answer", "source", etc.) are called "fields". This is analogous to rows and columns, respectively, in a SQL database. It's possible to delete documents as well as fields, but the process is quite different so I need to know which one you mean before I can answer.Cist
I think I would like to delete the "document". If I designate for example id==16, I want to delete whole JSON objects inside { } related to designated id. Expected result is that "search.score", "id", "questions", "answer", "source", "keywords" and "alternateQuestions" are totally deleted, while other documents are still alive. I apologize my English is not good, btw.Ambler
Thanks for clarifying!Cist
R
16

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.

Rysler answered 24/1, 2019 at 17:30 Comment(5)
Am getting a "The request is invalid. Details: actions : 0: Document key cannot be missing or empty." error message. Any suggestion?Gil
@ChandanYS Does this solve your problem? It must be the index key of azure search. #36392162Enteritis
@Enteritis Yeah it's resolved. id was not the index key in my case. Later I changed id as my Index Key then it worked.Gil
Is there any way to delete a document by field which is not a index key?Substantialize
I found this thread immensely helpful in formulating my document delete in Postman, but a correctly crafted delete validated by the instructions here and against the published api here: learn.microsoft.com/en-us/rest/api/searchservice/… with the admin key in header and the document key used as shown, i get a 405 Method Not Allowed Error. It is a top level response with no per document response json payload as detailed on the linked page. Any clue why this does not work?Brachylogy

© 2022 - 2024 — McMap. All rights reserved.