我在 azure 认知搜索索引中有一个名为文档名称的索引。该索引中的文档如下所示
{
"@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
}
]
}
]
}
我想根据 documentId 从此索引中删除文档。但是,documentId 不是该索引中的主键。Id 是主键。我在 Express 中使用 azure sdk 来实现此目的。 任何帮助将不胜感激。
我尝试过以下方法:
- 我对字段 documentId 进行搜索以首先获取文档,然后删除这些文档。 [由于不必要地获取文档而付出额外的努力]
- 尝试使用 action.delete 进行删除,但它需要主键,而 documentId 不是主键。
最佳答案
由于您使用的是 Azure 认知搜索,并且希望根据 documentId 删除文档,该 documentId 不是主键,但仍需要用于删除,因此您可以利用 Key-As-Payload 方法来删除文件。在此方法中,您可以将 documentId 设置为要删除的文档的主键 Id 字段的值。下面介绍了如何使用 Express 中的 Azure 认知搜索 SDK 实现此目的:
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);
关于azure - 从 Azure 认知搜索索引中删除文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76727328/