文檔的全文搜索

我們經常需要一個全文搜索 API,使我們的應用程序能夠在文檔中搜索指定為文本搜索查詢的特定信息。文檔可以是任何格式,例如 Word(Doc、Docx)、PDF、HTML、EPUB、電子表格(XLS、XLSX)、演示文稿(PPT、PPTX)、圖像和視頻。

GroupDocs.Search 是一個功能強大的全文搜索 API,可讓您在應用程序中搜索 超過 70 種文檔格式。為了能夠即時搜索數千個文檔,必須將它們添加到索引中。

開發人員為何使用 GroupDocs.Search?

  • 不需要額外的軟件來搜索支持格式的文檔。
  • 提供了多種索引和搜索選項以滿足任何要求。
  • 在文本或對象形式查詢中可以使用多種搜索類型。
  • 高索引和搜索性能是通過獨特的算法和數據結構、優化和多線程執行實現的。
  • 支持在文檔文本中以多種方式可視化搜索結果。

GroupDocs.Search API在搜索引擎分類中的位置請查看關於搜索引擎一文。

安裝

GroupDocs.Search for .NET 託管在 NuGet 上,可以使用 NuGet 包管理器輕鬆安裝。或者,您可以從 下載 部分下載 API 的 DLL。

使用 C# 搜索 Office 文檔

以下步驟說明瞭如何在多個文檔(Word、Excel、PDF 和其他文檔格式)中搜索單詞或短語。

  • 創建新索引:首先,您需要創建一個索引。索引可以在內存或磁盤上創建。退出程序後無法保存在內存中創建的索引。相反,在磁盤上創建的索引可能會在將來加載以繼續工作。 創建索引 部分描述了創建索引的詳細信息。
  • 訂閱索引事件:創建索引後,需要將文檔添加到索引中進行索引。索引文檔的成功或失敗可能有多種原因,例如,由於磁盤讀取錯誤或存在訪問文檔的密碼。要接收有關索引錯誤的信息,您可以訂閱 ErrorOccurred 事件。要使用事件,請參閱 搜索索引事件 部分。
  • 索引文檔:文檔索引可以同步或異步執行。同步索引意味著啟動索引過程的線程將處於忙碌狀態,直到操作完成。然而,更多的時候,需要異步執行索引,能夠在發起操作的線程中執行其他任務。 索引部分提供了索引過程各個方面的詳細描述。
  • 執行搜索:當文檔被索引時,索引已準備好處理搜索查詢。支持以下類型的搜索查詢:簡單、模糊、區分大小寫、布爾值、短語、分面、帶通配符等。 搜索 部分介紹了各種類型的搜索查詢的描述。
  • 使用搜索結果:搜索完成後,您需要以某種方式解釋結果。結果可以用找到的文檔的簡單列表來表示,或者可以在文檔的文本中突出顯示找到的單詞和短語。有關處理搜索結果的更多信息,請參閱搜索結果
string indexFolder = @"/Users/muhammadsohailismail/MyIndex/"; // Specify the path to the index folder
string documentsFolder = @"/Users/muhammadsohailismail/MyDocuments/"; // Specify the path to a folder containing documents to search

// a) Create new index or
// b) Open existing index
Index index = new Index(indexFolder);

// c) Subscribe to index events
index.Events.ErrorOccurred += (sender, args) =>
{
    Console.WriteLine(args.Message); // Writing error messages to the console
};

// d) Add files synchronously
index.Add(documentsFolder); // Synchronous indexing documents from the specified folder

// f) Perform search
string query = "Worthy"; // Specify a search query
SearchResult result = index.Search(query); // Searching in the index

// g) Use search results
// Printing the result
Console.WriteLine("Documents found: " + result.DocumentCount);
Console.WriteLine("Total occurrences found: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
    FoundDocument document = result.GetFoundDocument(i);
    Console.WriteLine("\\tDocument: " + document.DocumentInfo.FilePath);
    Console.WriteLine("\\tOccurrences: " + document.OccurrenceCount);
}

// Highlight occurrences in text
if (result.DocumentCount > 0)
{
    FoundDocument document = result.GetFoundDocument(0); // Getting the first found document
    string path = @"/Users/muhammadsohailismail/Output/Highlighted.html";
    OutputAdapter outputAdapter = new FileOutputAdapter(path); // Creating the output adapter to a file
    HtmlHighlighter highlighter = new HtmlHighlighter(outputAdapter); // Creating the highlighter object
    index.Highlight(document, highlighter); // Generating output HTML formatted document with highlighted search results

    Console.WriteLine();
    Console.WriteLine("Generated HTML file can be opened with Internet browser.");
    Console.WriteLine("The file can be found by the following path:");
    Console.WriteLine(Path.GetFullPath(path));
}

上面的代碼生成以下輸出和 HTML 文件

使用 C# 在文檔字段中搜索

分面搜索是通過設置要搜索的有效文檔字段名稱來過濾搜索結果。分面搜索允許您僅在文檔的某些字段中進行搜索,例如,僅在內容字段或文件名字段中進行搜索。下面展示了一個簡單的分面搜索示例,其中包含文本和對象形式的查詢。

string indexFolder = @"c:\\MyIndex\\";
string documentsFolder = @"c:\\MyDocuments\\";
 
// Creating an index in the specified folder
Index index = new Index(indexFolder);
 
// Indexing documents from the specified folder
index.Add(documentsFolder);
 
// Search in the content field with text query
SearchResult result1 = index.Search("content: Einstein");
 
// Search in the content field with object query
SearchQuery wordQuery = SearchQuery.CreateWordQuery("Einstein");
SearchQuery fieldQuery = SearchQuery.CreateFieldQuery(CommonFieldNames.Content, wordQuery);
SearchResult result2 = index.Search(fieldQuery);

使用格式特定字段

對於每種文檔格式,此類文檔中可能存在一些標準字段。該庫提供以下包含標准文檔字段名稱常量的類:EpubFieldNamesFictionBookFieldNamesMailFieldNamesPresentationFieldNamesSpreadsheetFieldNames , WordsFieldNames

還有一些字段可能出現在任何類型的文檔中。這些字段的名稱在 CommonFieldNames 類中表示。

以下示例展示了使用文檔的標準字段名稱的示例。

string indexFolder = @"c:\\MyIndex\\";
string documentsFolder = @"c:\\MyDocuments\\";
 
// Creating an index in the specified folder
Index index = new Index(indexFolder);
 
// Indexing documents from the specified folder
index.Add(documentsFolder);
 
// Search in the content field with text query
string query1 = WordsFieldNames.Company + ": Dycum";
SearchResult result1 = index.Search(query1);
 
// Search in the content field with object query
SearchQuery wordQuery = SearchQuery.CreateWordQuery("Dycum");
SearchQuery fieldQuery = SearchQuery.CreateFieldQuery(WordsFieldNames.Company, wordQuery);
SearchResult result2 = index.Search(fieldQuery);

有關分面搜索的詳細信息,請參見頁面 分面搜索

結論

本文解釋瞭如何在 C# 中搜索文檔(DOCX、PDF、Excel、文本文件)以獲取特定信息。它還解釋瞭如何在文檔字段中進行搜索。 GroupDocs.Search 包含其他幾個功能,請查看 文檔 了解更多信息。