高效管理搜索结果并使其易于识别是开发强大的文档搜索应用程序的关键方面。使用使用 C# 突出显示搜索结果,您可以通过在搜索结果中直观区分相关信息来增强用户体验。本指南将引导您完成使用 C# 突出显示搜索结果的过程,向您展示如何将视觉指示器应用于匹配的术语,使用户能够更轻松地快速有效地找到所需的信息。
使用 C# 突出显示搜索结果的步骤
- 通过集成 GroupDocs.Search for .NET 库来设置您的开发环境,以突出显示搜索结果
- 实例化 IndexSettings 对象来配置文档的索引方式
- 将 TextStorageSettings 设置为 Compression.High,以有效存储提取的文本
- 使用 Index 类创建索引。将索引文件夹的路径和 IndexSettings 对象传递给构造函数
- 使用 Index.Add 方法将指定文件夹中的文档添加到索引
- 使用 Index.Search 方法搜索特定关键字或短语,例如Universe”。这将返回一个 SearchResult 对象,其中包含与搜索查询匹配的所有文档
该过程从设置索引开始,索引用于存储从文档中提取的文本。此索引对于快速准确的搜索至关重要。使用 IndexSettings,我们启用高压缩文本存储以保持效率。创建 Index 对象,并使用指定文件夹中的文档填充该对象。索引文档后,您可以对特定关键字执行搜索操作。例如,要突出显示文档中出现的单词Universe”,我们首先在索引中搜索它。SearchResult 对象包含包含该关键字的所有文档。如果有任何匹配项,则获取 FoundDocument,并使用荧光笔应用突出显示。您可以通过设置 TermHighlightStartTag 和 TermHighlightEndTag 来自定义突出显示格式,以在文本中清晰显示匹配的术语。这种方法不仅允许您执行 C# 文档搜索和突出显示,还可以通过清晰标记相关文本匹配项来增强用户体验。
使用 C# 突出显示搜索结果的代码
using GroupDocs.Search; | |
using GroupDocs.Search.Common; | |
using GroupDocs.Search.Highlighters; | |
using GroupDocs.Search.Options; | |
using GroupDocs.Search.Results; | |
using System; | |
namespace HighlightSearchResultsUsingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Apply the license to remove the restrictions | |
// imposed by the Search library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Search.lic"); | |
string indexFolder = @"d:\MyIndex\"; | |
string documentsFolder = @"d:\MyDocuments\"; | |
// Creating an index settings instance | |
IndexSettings settings = new IndexSettings(); | |
// Enabling storage of extracted text in the index | |
settings.TextStorageSettings = new TextStorageSettings(Compression.High); | |
// Creating an index in the specified folder | |
Index index = new Index(indexFolder, settings); | |
// Indexing documents from the specified folder | |
index.Add(documentsFolder); | |
// Search for the word 'Universe' | |
SearchResult result = index.Search("Universe"); | |
// Highlighting occurrences in the text | |
if (result.DocumentCount > 0) | |
{ | |
// Getting the first found document | |
FoundDocument document = result.GetFoundDocument(0); | |
// Creating the output adapter | |
StructureOutputAdapter outputAdapter = new StructureOutputAdapter( | |
OutputFormat.PlainText); | |
// Creating the highlighter instance | |
Highlighter highlighter = new DocumentHighlighter(outputAdapter); | |
// Creating the highlight options | |
HighlightOptions options = new HighlightOptions(); | |
// Setting the start tag for the found word | |
options.TermHighlightStartTag = "<Term>"; | |
// Setting the end tag for the found word | |
options.TermHighlightEndTag = "</Term>"; | |
// Generating plain text with highlighted occurrences | |
index.Highlight(document, highlighter, options); | |
DocumentField[] fields = outputAdapter.GetResult(); | |
Console.WriteLine(document.ToString()); | |
for (int i = 0; i < fields.Length; i++) | |
{ | |
// Printing field names of the found document | |
DocumentField field = fields[i]; | |
Console.WriteLine("\t" + field.Name); | |
} | |
} | |
} | |
} | |
} |
在 C# 中突出显示搜索匹配项是文档搜索应用程序的一项基本功能,可让用户更轻松地快速找到相关信息。通过使用 GroupDocs.Search 等库,您可以实现一个功能强大且高效的系统,该系统支持在 C# 中突出显示搜索匹配项。这种方法不仅增强了搜索功能,而且还使结果在视觉上与众不同,从而改善了用户交互。使用 C# 实现搜索和突出显示非常简单,并且可以根据特定的应用程序需求进行定制,无论您是在开发文档管理系统还是实时搜索工具。
之前,我们发布了有关使用 C# 获取索引文档的详细指南。如需完整的分步教程,请务必查看我们有关如何 使用 C# 获取索引文档 的详细文章。