Sanjay
Sanjay

Reputation: 63

How to delete a document from azure search once the document in cosmos db expires?

I am currently using Azure Search Service whose data source is an Azure Cosmos DB. Currently, when the TTL on a document in Cosmos DB expires the corresponding document in the Search Service remains. How do I make sure the document is deleted from the Search Service as well?

I am aware that I can provide a field name to the 'Soft delete column' in the data source which will prevent that document from getting indexed but in my case, the document is not manually deleted (rather when the TTL expires) therefore the soft delete column cannot be set.

Upvotes: 0

Views: 947

Answers (1)

Michael Scott
Michael Scott

Reputation: 580

You can work with Azure Cosmos DB change feed to arrive at a solution to your problem. You can create an Azure Function that gets triggered whenever there is a change to your documents in cosmosDB. In the Azure function, you can write the logic to delete the document from cognitive search using REST apis.

https://learn.microsoft.com/en-us/azure/cosmos-db/read-change-feed#azure-functions

This is a much cleaner approach compared to running the indexer again. If this logic doesn't work for you for whatever reason, you can use run the indexer as proposed by @gaurav mantri

Upvotes: 0

Related Questions