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

um6iljoc  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个索引在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上运行了搜索以获取文档,然后删除了这些文档。[由于不必要的文件获取而需要额外的努力]
1.尝试使用action.delete删除,但它需要主键,而documentId不是主键。

ia2d9nvy

ia2d9nvy1#

由于您使用的是Azure认知搜索,并且希望根据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);

字符串

相关问题