Reputation: 29
I have an index in azure cognitive search index by the name of document. A document in this index looks like this
{
"@search.score": 1,
"id": "3412b974-ca9e-4bce-a592-78084a71e4c6",
"content": null,
"documentId": "3d8c0912-e292-483f-80a2-5e63c57b9504",
"bucket": "test-storage-autonomize",
"name": "demo-project/75131_Patient.pdf",
"metadata": "",
"form": [],
"table": [],
"medicalEntities": [
{
"BeginOffset": 1961,
"Category": "MEDICAL_CONDITION",
"EndOffset": 1964,
"Id": 56,
"Score": 0.808318018913269,
"Text": "ARF",
"Type": "DX_NAME",
"Attributes": [],
"ICD10CMConcepts": [
{
"Code": "J96.0",
"Description": "Acute respiratory failure",
"Score": 0.8527929592132568
},
{
"Code": "J96.0",
"Description": "Acute respiratory failure",
"Score": 0.8527929592132568
},
{
"Code": "J96.2",
"Description": "Acute and chronic respiratory failure",
"Score": 0.8522305774688721
},
{
"Code": "N17.2",
"Description": "Acute kidney failure with medullary necrosis",
"Score": 0.8506965065002441
},
{
"Code": "N17.2",
"Description": "Acute renal failure with medullary necrosis",
"Score": 0.8506684684753418
}
],
"RxNormConcepts": [],
"CPT_Current_Procedural_Terminology": [],
"Traits": [
{
"Name": "SIGN",
"Score": 0.808318018913269
}
]
}
]
}
I want to delete documents from this index based upon documentId. But, documentId is not the primary key in this index.Id is the primary key. I am using azure sdk for this in express. Any help would be appreciated.
I have tried the following approaches:
Upvotes: 1
Views: 783
Reputation: 1327
Since you're using Azure Cognitive Search and you want to delete documents based on the documentId, which is not the primary key but still needs to be used for deletion, you can utilize the Key-As-Payload approach to delete the documents. In this method, you can set the documentId as the value of the primary key Id field for the documents you want to delete. Here's how you can achieve this using the Azure Cognitive Search SDK in Express:
const { SearchClient, AzureKeyCredential } = require("@azure/search-documents");
// Replace the following values with your actual Azure Cognitive Search credentials and configuration
const searchServiceName = "your-search-service-name";
const indexName = "your-index-name";
const apiKey = "your-admin-api-key"; // Ensure that this key has delete permissions
const credentials = new AzureKeyCredential(apiKey);
const searchClient = new SearchClient(searchServiceName, indexName, credentials);
async function deleteDocumentByDocumentId(documentId) {
// Use documentId as the value of the 'Id' field for deletion
const documentToDelete = { Id: documentId };
try {
await searchClient.deleteDocuments([documentToDelete]);
console.log(`Document with documentId ${documentId} deleted successfully.`);
} catch (err) {
console.error(`Error deleting document with documentId ${documentId}:`, err);
}
}
// Call the deleteDocumentByDocumentId function with the desired documentId to delete
const documentIdToDelete = "3d8c0912-e292-483f-80a2-5e63c57b9504";
deleteDocumentByDocumentId(documentIdToDelete);
Upvotes: -2