Shivesh Narain
Shivesh Narain

Reputation: 11

How to delete a specific document from azure search index?

I have a specific number of documents from the azure search index to be deleted and I need a solution in python for the same. I created an index in azure search already and the format of the index is given below

> {
>     "@odata.context": "https://{name}.search.windows.net/indexes({'index name'})/$metadata#docs(*)",
>     "value": [
>         {
>             "@search.score": ,
>             "content": "",
>             "metadata_storage_name": "",
>             "metadata_storage_path": "",
>             "metadata_storage_file_extension": "",}]}

metadata_storage_path is the unique key for each document in azure search index.

I got 2 ways to go about the problem using azure-python SDK and python request module but both the methods are throwing me an error which is listed below.

method - 1 (using python request module)

I got the reference from azure documentation

https://learn.microsoft.com/en-us/rest/api/searchservice/addupdate-or-delete-documents

import json
import requests
api_key = "B346FEAB56E6D5*******"
headers = {
  'api-key': f'{api_key}',
  'Content-Type': 'application/json'
}
doc_idx = "Index name"
doc_url = f"https://{name}.search.windows.net/indexes/{doc_idx}-index/docs/search?api-version=2020-06-30-Preview"

payload = json.dumps({

"@search.action": "delete",
"key_field_name":({"metadata_storage_path": "aHR0cHM6Ly9mc2NvZ******"})
},
)
response = json.loads(requests.request("POST", doc_url, headers=headers, data=payload).text)
print(response)

I am getting the following error.

{'error': {'code': '',
  'message': "The request is invalid.## Heading ## Details: parameters : The parameter 'key_field_name' in the request payload is not a valid parameter for the operation 'search'.\r\n"}}

I also tried manipulating the code but I am not able to make it work please let me know weather i am making some mistake in the code or is there some issues with the python request module and azure search.

Method - 2 (using azure python SDK)

I got the Reference from azure documentation.

https://learn.microsoft.com/en-us/python/api/azure-search-documents/azure.search.documents.searchclient?view=azure-python

I tried to delete one document inside the azure search index with azure python SDK and the code is given below.

from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient

key = AzureKeyCredential('B346FEAB******')
doc_idx = "index name"
service_endpoint  = f"https://{name}.search.windows.net/indexes/{doc_idx}-index/docs/search?api-version=2020-06-30-Preview"
search_client = SearchClient(service_endpoint, doc_idx , key,)

# result = search_client.delete_documents(documents=[DOCUMENT])

result = search_client.delete_documents(documents=[{"metadata_storage_name": "XYZ.jpg"}])

print("deletion of  document succeeded: {}".format(result[0].succeeded))

I am getting the following error.

ResourceNotFoundError                     Traceback (most recent call last)
<ipython-input-7-88beecc15663> in <module>
     13 # result = search_client.upload_documents(documents=[DOCUMENT])
     14 
---> 15 result = search_client.delete_documents(documents=[{"metadata_storage_name": "XYZ.jpg"}])----------------------------------------------

ResourceNotFoundError: Operation returned an invalid status 'Not Found'

I also tried using metadata_storage_path instead of metadata_storage_name and I got the same error.

please check the code and let me know where I am making mistake and also if there is any other method for deleting a specific document in azure search index.

Upvotes: 1

Views: 4571

Answers (2)

NEERAJ R
NEERAJ R

Reputation: 21

from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient

key = AzureKeyCredential('******')
doc_idx = "resumes"
service_endpoint  = "****"
search_client = SearchClient(service_endpoint, doc_idx , key,)
result = search_client.delete_documents(documents=[{"id": "aHR0cHM6Ly9saWJhY2tlbmRzdG9yYWdlLmJsb2IuY29yZS53aW5kb3dzLm5ldC9yZXN1bWVzL1N1a2hpbF9RQS5wZGY1"}])
print("deletion of  document succeeded: {}".format(result[0].succeeded))

same code i tried it is working perfectly fine for me need to verify below given scenarios,

  1. Check whether the index name is correct.
  2. Index name should not contain space.
  3. Check that data is present in the index.
  4. Confirm metadata_storage_name is the key.

Upvotes: 1

Dan G&#248;ran Lunde
Dan G&#248;ran Lunde

Reputation: 5353

You have not defined a variable for name.

service_endpoint  = f"https://{name}.search.windows.net/indexes/{doc_idx}-index/docs/search?api-version=2020-06-30-Preview"

Becomes

https://.search.windows.net/indexes/Index%20name-index/docs/search?api-version=2020-06-30-Preview

Upvotes: 0

Related Questions