azure - 从 Azure 认知搜索索引中删除文档

标签 azure azure-cognitive-services azure-cognitive-search

我在 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 来实现此目的。 任何帮助将不胜感激。

我尝试过以下方法:

  1. 我对字段 documentId 进行搜索以首先获取文档,然后删除这些文档。 [由于不必要地获取文档而付出额外的努力]
  2. 尝试使用 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/

相关文章:

azure - 文本翻译 API 3.0 : Transliterate

javascript - 如何使用javascript在azure文本识别API中发布图像而不是URL?

azure - Bot 框架 Composer 错误 : Unable to get a value for KnowledgeBaseId from state

Azure 搜索建议 API - 无序关键字没有结果

c# - Azure 搜索 .Net SDK 中的复杂类型支持

azure - 构建帐户 SAS(共享访问签名)

azure - 无法解析 Azure Key Vault 的远程名称

python - Azure 发音评估 SDK 与 api 调用相比返回错误结果

azure - 在 Azure 搜索中重新索引后分页

c# - 在 Visual Studio 中更改 SQL Server 数据库最大大小以便在 Azure 中上传