使用 GroupDocs.Redaction 创建搜索索引 .NET:索引和管理别名以实现安全文档管理

在管理大量文档的同时保持敏感信息机密可能具有挑战性。使用 GroupDocs.Redaction for .NET,您可以 create search index .NET 项目,对文档集合进行脱敏和搜索。本文教程将指导您使用 Aspose.Search.NET 创建或打开索引、向索引添加文档、管理别名字典以及集成脱敏功能——同时保持严格的数据安全。

快速答案

  • 本指南的主要目的是什么? To show you how to create search index .NET projects, add documents to the index, and manage aliases with GroupDocs.Redaction.
  • 需要哪些库? GroupDocs.Redaction and Aspose.Search.NET, both available via NuGet.
  • 我需要许可证吗? A free trial works for evaluation; a full license is required for production.
  • 我可以处理大型文档集吗? Yes—both libraries support processing multi‑hundred‑page files without loading the entire file into memory.
  • 该解决方案兼容 .NET‑Core 吗? Absolutely; it runs on .NET 5, .NET 6, and later versions.

介绍

在深入之前,让我们阐明为什么 creating a search index .NET 解决方案很重要。索引可以让您在毫秒内定位数千个文件中的精确短语、模式或已脱敏内容,显著加快合规审查、法律取证和内部审计。通过将 Aspose.Search.NET 强大的索引引擎与 GroupDocs.Redaction 稳健的脱敏工具相结合,您获得了一个既能保护又能即时查找数据的单一管道。

什么是 GroupDocs.Redaction for .NET?

GroupDocs.Redaction for .NET 是一个库,可对超过 30 种文档格式(包括 PDF、DOCX、PPTX 和 HTML)中的文本、图像和元数据进行编程式脱敏。它可处理高达 2 GB 的文件而无需将整个文档加载到内存中,确保在企业工作负载下实现高性能操作。

为什么使用 Aspose.Search.NET 创建搜索索引 .NET?

Aspose.Search.NET 能够索引 50+ 种文件类型并支持增量更新,这意味着您可以向现有索引添加新文件而无需重新构建。与完整重新索引相比,这可将 CPU 使用率降低至最高 70 %,并且对于包含 500 k+ 文档的索引,查询响应时间保持在 200 ms 以下。

前提条件

必需的库、版本和依赖项

  • GroupDocs.Redaction – 最新的 NuGet 发行版,兼容 .NET 5/6/7。
  • Aspose.Search.NET – 最新的 NuGet 发行版,支持 .NET Standard 2.0+ 和 .NET Core。

两个库必须针对相同的 .NET 运行时版本,以避免绑定冲突。

环境设置要求

  • Visual Studio 2022(或任何支持 .NET 6+ 的 IDE)。
  • 访问包含您希望索引和脱敏的文档的文件夹。

知识前提

  • 熟悉 C# 语法和 .NET 项目结构。
  • 索引、搜索和文档脱敏的基本概念。

如何创建搜索索引 .NET?

加载或实例化一个 Index 对象,指向一个文件夹,并调用 CreateOrOpen——这一步即可在磁盘上创建或重新打开可搜索的存储。该操作默认在后台线程中运行,因此即使在索引数千个文件时,您的 UI 也保持响应。

Index 表示存储在磁盘上的可搜索集合。

定义锚点

Index 类是 Aspose.Search.NET 的核心组件,表示磁盘上文档的可搜索集合。所有索引和查询操作均通过此对象进行。

dotnet add package GroupDocs.Redaction

如何向索引添加文档?

AddDocument 将文件添加到索引并提取其可搜索内容。

通过对每个文件路径调用 Index.AddDocument 来添加文档;该方法会自动提取文本、元数据和可选的 OCR 内容。您可以在 foreach 循环中批量添加文件,索引将增量更新而无需重建现有条目。该过程还返回一个状态对象,指示成功或任何错误,便于您以编程方式处理失败。

Install-Package GroupDocs.Redaction

获取许可证的步骤

  • 免费试用 – 免费探索所有功能,期限 30 天。
  • 临时许可证 – 使用限时密钥进行扩展测试。
  • 完整许可证 – 生产部署所需,移除所有评估限制。

安装后,按如下方式初始化 GroupDocs.Redaction:

using GroupDocs.Redaction;
RedactorSettings settings = new RedactorSettings();
// Set up additional configuration as needed

实施指南

我们将根据功能将实现分解为可管理的章节。

创建或打开索引

概述

创建或打开搜索索引是高效文档管理和搜索的基础。这使您能够快速处理已索引的数据。

步骤说明

1. 创建/打开索引

using GroupDocs.Search;

// Specify the directory where your index will be stored
string indexPath = "YOUR_INDEX_DIRECTORY";

// Initialize or open an existing index
Index index = new Index(indexPath);

// Explanation: The Index object is initialized with a directory path,
// which serves as a storage location for all indexed data.

2. 向索引添加文档

using GroupDocs.Search;

// Specify your document directory
string docPath = "YOUR_DOCUMENT_DIRECTORY";

// Add documents from the specified directory to the index
index.Add(docPath);

// Explanation: The Add method indexes all documents within the provided path,
// making them searchable.

管理别名字典

概述

别名字典中的别名允许您为复杂的搜索查询定义简写术语,提高搜索效率和可读性。

步骤说明

1. 清除现有别名

using GroupDocs.Search.Dictionaries;

// Check if there are existing aliases and clear them
if (index.Dictionaries.AliasDictionary.Count > 0)
{
    index.Dictionaries.AliasDictionary.Clear();
}

// Explanation: This ensures you start with a clean slate when managing aliases.

2. 添加单个别名条目

index.Dictionaries.AliasDictionary.Add("t", "(gravida OR promotion)");
index.Dictionaries.AliasDictionary.Add("e", "(viverra OR farther)");

// Explanation: Each alias is mapped to an expression that defines its search scope.

3. 使用数组添加多个别名

AliasReplacementPair[] pairs = new AliasReplacementPair[]
{
    new AliasReplacementPair("d", "daterange(2017-01-01 ~~ 2019-12-31)"),
    new AliasReplacementPair("n", "(400 ~~ 4000)")
};

index.Dictionaries.AliasDictionary.AddRange(pairs);

// Explanation: AddRange allows bulk addition of aliases, streamlining the setup process.

检索和导出别名

概述

将别名字典导出到文件可让您在团队或环境之间共享搜索词汇,而检索特定条目则有助于验证查询逻辑。

检索别名

if (index.Dictionaries.AliasDictionary.Contains("e"))
{
    string replacement = index.Dictionaries.AliasDictionary.GetText("e");
}

// Explanation: Use Contains to check existence before retrieval.

导出别名

string exportPath = "YOUR_OUTPUT_DIRECTORY/Aliases.dat";
index.Dictionaries.AliasDictionary.ExportDictionary(exportPath);

// Explanation: ExportDictionary saves the alias dictionary for future use or sharing.

导入别名并使用别名字典搜索

概述

从文件导入别名,以使用预定义的简写术语简化搜索操作,然后运行引用这些别名的查询。

1. 导入别名

string importPath = "YOUR_OUTPUT_DIRECTORY/Aliases.dat";
index.Dictionaries.AliasDictionary.ImportDictionary(importPath);

// Explanation: This method loads existing alias definitions into the dictionary.

2. 使用别名搜索

string query = "@t OR @e"; // Use aliases in your search query
SearchResult result = index.Search(query);

// Explanation: Leverage aliases to perform complex searches with simple queries.

常见问题及解决方案

  • 索引路径错误 – 确保文件夹路径为绝对路径且应用程序具有读/写权限。
  • 内存泄漏 – 使用后始终对 Index 对象调用 Dispose(),尤其是在长期运行的服务中。
  • 别名未识别 – 验证别名文件使用 UTF‑8 编码,并且每行遵循 key=value 格式。

常见问答

Q: 我可以将此解决方案与 .NET Core 一起使用吗?
A: 是的,GroupDocs.Redaction 和 Aspose.Search.NET 完全支持 .NET Core 3.1、.NET 5、.NET 6 以及更高版本。

Q: 索引可以处理多少文档?
A: 该引擎已在包含超过 1 百万文档的索引上进行测试,在标准服务器硬件上保持亚秒级查询时间。

Q: 别名支持正则表达式吗?
A: 别名可以包含通配符字符(* 和 ?),但不支持完整的正则表达式;对于复杂模式,请以编程方式构建查询。

Q: 可以在搜索后进行脱敏吗?
A: 完全可以。从搜索结果中检索文档 ID,使用 GroupDocs.Redaction 加载它,应用脱敏规则,并保存受保护的版本。

Q: 大型企业适用什么许可模式?
A: GroupDocs 提供基于数量的许可,允许无限数量的开发者和部署站点;请联系销售获取定制报价。

结论

通过掌握 GroupDocs.Redaction 与 Aspose.Search.NET 的集成,以 create search index .NET 解决方案,您可以显著提升文档安全性、合规性和可发现性。您现在拥有构建索引、向索引添加文档、管理别名字典以及应用脱敏规则的工具——全部在单个高性能 .NET 应用程序中完成。

下一步?在测试项目中实现代码片段,尝试自定义别名模式,并针对生产数据集对索引速度进行基准测试。


最后更新: 2026-07-02
测试环境: GroupDocs.Redaction 23.10 for .NET, Aspose.Search.NET 24.5
作者: GroupDocs

相关教程