【问题标题】:.Net Core Azure cognitive search delete document.Net Core Azure 认知搜索删除文档
【发布时间】:2023-01-05 07:10:08
【问题描述】:

我在我的 .Net Core 3.1 应用程序中使用 Azure.Search.Documents 包来实现 azure 认知搜索。我有要求必须从文档中删除现有数据。我通过参考文档尝试了几种方法,但都没有用。

我尝试了以下方法

方法一

var searchIndexClient = new SearchIndexClient(new Uri(<URI>), new AzureKeyCredential("XYZ"));

var searchClient = searchIndexClient.GetSearchClient(indexName);
var options = new IndexDocumentsOptions { ThrowOnAnyError = true };
var res = await searchClient.DeleteDocumentsAsync("Id", new List<string> { "1", "2", "3"}, options);

方法二:

var searchIndexClient = new SearchIndexClient(new Uri(<URI>), new AzureKeyCredential("XYZ"));

var searchClient = searchIndexClient.GetSearchClient(indexName);

var batch = IndexDocumentsBatch.Delete("Id", documents);

var options = new IndexDocumentsOptions { ThrowOnAnyError = true };
var res = await searchClient.IndexDocumentsAsync(batch, options);

当我尝试这些方法时,出现以下错误:

{
    "error": {
        "code": "MissingRequiredParameter",
        "message": "The request is invalid. Details: actions : No indexing actions found in the request. Please include between 1 and 32000 indexing actions in your request.",
        "details": [{
                "code": "MissingIndexDocumentsActions",
                "message": "No indexing actions found in the request. Please include between 1 and 32000 indexing actions in your request. Parameters: actions"
            }
        ]
    }
}

感谢任何帮助,注意:我只想使用 SDK 而不是 Rest API

【问题讨论】:

    标签: c# azure .net-core azure-cognitive-search


    【解决方案1】:

    DeleteDocumentsAsync 将删除一个完整文档或批量删除一个文档。 IndexDocumentsBatch.Delete会根据索引中的key或字符串值进行批量删除文档。

    这些方法都不会删除文档字段值,只会删除完整文档。如果您需要从文档中删除现有字段,请考虑 IndexDocumentsAction.Merge 并确保您要删除的字段为空,以便合并在必填字段中添加空值并保留其余字段。

    更多信息在这里:https://learn.microsoft.com/azure/search/search-howto-dotnet-sdk

    【讨论】:

    • 嗨@Gia Mondragon - MSFT,我想删除具有给定 Id 的文档,我正在使用 IndexDocumentsBatch.Delete 但它给了我上述错误
    • 您正在尝试使用 IndexDocumentOptions 而您需要使用 IndexDocumentsAction,在本例中为 IndexDocumentsAction.DeleteHere is has a sample 关于如何使用 IndexDocumentsAction.Upload 执行此操作,如果您需要进一步的帮助来修复错误,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2021-03-26
    • 1970-01-01
    • 2016-07-01
    • 2020-06-17
    相关资源
    最近更新 更多