【问题标题】:How to remove all documents in index如何删除索引中的所有文档
【发布时间】:2015-04-04 07:22:03
【问题描述】:

是否有任何简单的方法可以从 Azure 搜索索引中删除所有文档(或过滤列表或文档)?

我知道显而易见的答案是删除并重新创建索引,但我想知道是否还有其他选择。

【问题讨论】:

    标签: azure-cognitive-search


    【解决方案1】:

    不,目前无法从索引中删除所有文档。正如您所怀疑的那样,删除并重新创建索引是要走的路。对于非常小的索引,您可以考虑单独删除文档,但考虑到应用程序通常已经有用于创建索引的代码,删除/重新创建是最快的路径。

    【讨论】:

    • 如果您可以将此作为一个项目添加到我们的 UserVoice 页面 (feedback.azure.com/forums/263029-azure-search),那就太好了,因为我们已经多次听到这个问题,跟踪它的重要性将非常有帮助鉴于 Pablo 的解决方法,为您自己和其他人。我们可能会考虑的一种选择是还允许使用 $filter 进行删除,这样您不仅可以删除所有文档,还可以删除特定文档。
    【解决方案2】:

    有一个办法:查询所有文档,用IndexBatch把这些家伙删掉。

        public void DeleteAllDocuments()
        {
            // Get index
            SearchIndexClient indexClient = new SearchIndexClient(SearchServiceName, SearchServiceIndex, new SearchCredentials(SearchServiceQueryApiKey));
    
            // Query all
            DocumentSearchResult<Document> searchResult;
            try
            {
                searchResult = indexClient.Documents.Search<Document>(string.Empty);
            }
            catch (Exception ex)
            {
                throw new Exception("Error during AzureSearch");
            }
    
            List<string> azureDocsToDelete =
                searchResult
                    .Results
                    .Select(r => r.Document["id"].ToString())
                    .ToList();
    
            // Delete all
            try
            {
                IndexBatch batch = IndexBatch.Delete("id", azureDocsToDelete);
                var result = indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException ex)
            {
                throw new Exception($"Failed to delete documents: {string.Join(", ", ex.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))}");
            }
        }
    

    【讨论】:

    • 这有两个主要问题: 1. 在一般情况下,不可能以一致的方式枚举大型索引中的所有文档。 2. 由于删除实际上是在 Lucene 级别进行的,因此效率非常低。 “删除”文档只是将其标记为已删除,以便在合并段时它最终会消失。大量这样做会为段合并创建大量工作,并在短期内消耗更多存储而不是释放它。 Pablo 的答案是最好的——删除整个索引并重新创建它。
    【解决方案3】:
        //searchIndex - Index Name
        //KeyCol - Key column in the index
        public static void ResetIndex(string searchIndex, string KeyCol)
        {
            while (true)
            {
                SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, searchIndex, new SearchCredentials(apiKey));
                DocumentSearchResult<Document> searchResult = indexClient.Documents.Search<Document>(string.Empty);
                List<string> azureDocsToDelete =
                searchResult
                    .Results
                    .Select(r => r.Document[KeyCol].ToString())
                    .ToList();
                if(azureDocsToDelete.Count != 0)
                {
                    try
                    {
                        IndexBatch batch = IndexBatch.Delete(KeyCol, azureDocsToDelete);
                        var result = indexClient.Documents.Index(batch);
                    }
                    catch (IndexBatchException ex)
                    {
                        throw new Exception($"Failed to reset the index: {string.Join(", ", ex.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))}");
                    }
                }
                else
                {
                    break;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      相关资源
      最近更新 更多