完整文字搜尋 Java – 精通 GroupDocs.Search Java – 建立與管理搜尋索引
在當今資料驅動的應用程式中,對大型文件集合執行高效的 full text search java 是必備功能。無論您是建置內部文件入口網站、電子商務目錄,或是內容豐富的 CMS,良好結構的搜尋索引都能提供快速、精確的結果。本教學將一步步說明如何設定 GroupDocs.Search for Java、建立可搜尋的索引、adding documents to index,以及執行 full text search java 查詢,全部以友善的步驟說明方式呈現。
快速解答
- What does “full text search java” mean? 在 Java 應用程式中對使用 GroupDocs.Search 建立的索引執行文字搜尋。
- Which library handles the indexing? GroupDocs.Search for Java(最新穩定版)。
- Do I need a license to try it? 提供免費試用;在正式環境需要臨時或完整授權。
- Can I index an entire folder at once? 可以 – 使用
index.add("folderPath")於一次呼叫中 add folder to index。 - Is the search case‑insensitive? 預設情況下,GroupDocs.Search 執行不區分大小寫的全文搜尋。
什麼是 full text search java?
full text search java 操作讓您能找出包含特定文字模式的文件。您將查詢字串傳遞給 GroupDocs.Search Index 物件的 search() 方法,函式庫會解析查詢、掃描已索引的詞彙,並即時回傳符合的文件。這使得在數千個檔案中快速取得相關內容成為可能,無需手動掃描。
為什麼要使用 GroupDocs.Search for Java?
GroupDocs.Search for Java 提供高效能、即插即用的索引與搜尋解決方案。它能索引 30+ 種文件格式,包括 PDF、DOCX、XLSX、PPTX、TXT、HTML 以及多種影像類型,且在處理上百頁的檔案時不需將整個檔案載入記憶體。內建演算法即使在索引包含 數百萬文件 時,也能提供 毫秒級 的回應時間,適用於小型工具與大型企業部署。
先決條件
在開始之前,請確保您已具備:
- Java Development Kit (JDK) 8+ – 用於編譯與執行程式碼的執行環境。
- Maven – 用於相依管理(您也可以使用 Gradle,但此處提供 Maven 範例)。
- 基本的 Java 類別、方法與命令列操作熟悉度。
設定 GroupDocs.Search for Java
Maven 設定
將 GroupDocs 倉庫與相依加入您的 pom.xml。這是您唯一需要變更的專案設定。
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/search/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-search</artifactId>
<version>25.4</version>
</dependency>
</dependencies>
直接下載(可選)
如果您不想使用 Maven,可從官方發行頁面取得最新 JAR: GroupDocs.Search for Java releases。
授權取得
- Free trial: 適合評估功能。
- Temporary license: 用於延長測試且無需承諾。
- Full license: 推薦於正式環境部署。
基本初始化
以下程式碼片段會建立一個空的索引資料夾。它是之後所有 full text search java 的基礎。
Index 是代表儲存在磁碟上的可搜尋索引的核心類別。
import com.groupdocs.search.Index;
public class GroupDocsSearchSetup {
public static void main(String[] args) {
String indexFolder = "YOUR_OUTPUT_DIRECTORY/HelloWorld";
Index index = new Index(indexFolder);
System.out.println("Index created at: " + indexFolder);
}
}
如何建立搜尋索引
直接答案
建立指向磁碟資料夾的 Index 實例,然後呼叫 index.add() 以匯入檔案。此單一操作會建構可搜尋的結構,支援所有後續的全文查詢。一次性初始化索引並批次加入文件,可確保最佳效能,避免重複重新索引。
Index 是管理可搜尋資料結構的中心類別。
import com.groupdocs.search.Index;
public class CreateIndexExample {
public static void main(String[] args) {
// Define the path for the output index directory
String indexFolder = "YOUR_OUTPUT_DIRECTORY/HelloWorld";
// Creating an index in the specified folder.
Index index = new Index(indexFolder);
System.out.println("Index created at: " + indexFolder);
}
}
將資料夾加入索引
索引已建立後,您需要 add documents to index,讓它們可被搜尋。GroupDocs.Search 能以單一次呼叫匯入整個資料夾。
add 方法會遞迴掃描指定目錄,從每個支援的檔案中抽取文字,並將詞彙儲存於索引中。
import com.groupdocs.search.Index;
public class AddDocumentsToIndexExample {
public static void main(String[] args) {
// Define the path for the output index directory and documents folder
String indexFolder = "YOUR_OUTPUT_DIRECTORY/HelloWorld";
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY";
// Create an index in the specified folder.
Index index = new Index(indexFolder);
// Adding documents from the specified folder to the index.
index.add(documentsFolder);
System.out.println("Documents added to index.");
}
}
執行 full text search java
文件已索引後,執行 full text search java 非常簡單。
search 方法接受查詢字串,使用已設定的分析器處理,並回傳包含符合文件的 SearchResult。
import com.groupdocs.search.Index;
import com.groupdocs.search.results.SearchResult;
public class SearchIndexExample {
public static void main(String[] args) {
// Define the path for the output index directory
String indexFolder = "YOUR_OUTPUT_DIRECTORY/HelloWorld";
// Create an index in the specified folder.
Index index = new Index(indexFolder);
// Performing a search query on the indexed documents.
SearchResult result = index.search("Lorem");
System.out.println("Search completed. Number of results: " + result.getDocumentCount());
}
}
實務應用
GroupDocs.Search for Java 在多種真實情境中表現優異:
- Internal document management systems – 即時取得政策、合約與手冊。
- E‑commerce platforms – 在擁有數千項目的目錄中快速搜尋商品。
- Content management systems (CMS) – 讓編輯與訪客快速定位文章、媒體與 PDF。
效能考量
為了讓您的 full text search java 保持閃電般快速:
- Optimize indexing: 僅重新索引變更的檔案,並定期清除過時條目。
- Manage resources: 監控 JVM 堆積使用情況;對大量資料集考慮增量索引。
- Follow best practices: 盡可能使用批次
add()呼叫,而非逐一加入檔案。
常見問題與解決方案
| 症狀 | 可能原因 | 解決方式 |
|---|---|---|
| 沒有返回結果 | 索引未建立或文件未加入 | 確認 index.add() 已成功執行;檢查資料夾路徑。 |
| 記憶體不足錯誤 | 同時載入過大的檔案 | 啟用增量索引或增加 JVM 堆積 (-Xmx)。 |
| 搜尋遺漏詞彙 | 分析器未針對語言設定 | 使用適當的 IndexSettings 設定語言專屬分析器。 |
常見問答
Q: GroupDocs.Search 能索引哪些檔案格式?
A: PDF、DOC/DOCX、XLS/XLSX、PPT/PPTX、TXT、HTML 等多種常見辦公格式——共計 30+ 種受支援類型。
Q: 我可以在遠端伺服器上執行 full text search java 嗎?
A: 可以。先在伺服器上建立索引,然後提供一個 REST 端點將查詢轉發至 Java 服務。
Q: 文件變更時要如何更新索引?
A: 使用 index.update("path/to/changed/file") 取代舊條目,無需重新建立整個索引。
Q: 有辦法將搜尋結果限制在特定資料夾嗎?
A: 取得 SearchResult 後,可依原始路徑過濾 result.getDocuments()。
Q: GroupDocs.Search 支援模糊或萬用字元搜尋嗎?
A: 函式庫內建支援模糊匹配 (~) 與萬用字元 (*) 運算子。
Q: 如何提升 full text search java 的相關性排序?
A: 調整 IndexSettings 如詞彙加權、提升特定欄位或啟用同義詞,以微調相關性。
Q: 可以從索引中刪除文件嗎?
A: 可以。呼叫 index.delete("path/to/file") 即可移除文件條目。
Q: “add folder to index” 實際上會做什麼?
A: GroupDocs.Search 會遞迴掃描資料夾,從每個支援的檔案抽取文字,將詞彙分詞後儲存於索引結構,以供快速查找。
Last updated: 2026-10-17
Tested with: GroupDocs.Search 25.4 for Java
Author: GroupDocs