如何在 Java 中搜尋多種檔案類型並建立索引文件
在現代企業中,您常常需要 搜尋多種檔案類型,同時將機密檔案以密碼鎖定。本教學示範如何使用 GroupDocs.Search 在 Java 中建立可搜尋的索引,以處理受密碼保護的文件。您將會看到如何設定密碼字典、處理需要密碼的事件,以及為大量批次優化效能——全部不需自行撰寫解密程式碼。
快速解答
- What does this tutorial cover? 使用密碼字典和事件監聽器對受密碼保護的文件進行索引。
- Which library is required? GroupDocs.Search for Java(最新版本)。
- Do I need a license? 可取得暫時的免費試用授權以供評估。
- Can I index other file types? 可以,GroupDocs.Search 支援超過 120 種格式,包括 PDF、DOCX、XLSX、PPTX 以及影像。
- What Java version is needed? JDK 8 或更新版本。
「create document index java」是什麼?
create document index java 指的是建立可搜尋資料結構的過程,該結構將詞彙映射到 Java 管理的檔案位置。使用 GroupDocs.Search 時,搜尋引擎會自動從支援的格式(包括加密的 PDF、Office 文件與影像)中抽取文字,讓您在不撰寫任何解密程式碼的情況下執行全文搜尋。此方式簡化了搜尋實作,同時保護文件安全。
為何使用 GroupDocs.Search 處理受密碼保護的檔案?
GroupDocs.Search 提供零接觸的解鎖機制,會從字典或事件處理器套用密碼,讓您在單一次索引過程中處理加密檔案。它在標準 8 核心伺服器上每小時可處理高達 200 萬份文件,支援 120+ 種檔案類型,並提供包含布林、萬用字元與模糊運算子的豐富查詢語言——這些都是建構跨多元內容可搜尋索引的關鍵。
前置條件
- 已安裝 Java Development Kit (JDK) 8+,並加入
PATH。 - IntelliJ IDEA 或 Eclipse 等 IDE。
- 用於相依管理的 Maven。
- GroupDocs.Search for Java – 透過 Maven 新增函式庫(請參考設定章節)。
設定 GroupDocs.Search for java
使用 Maven
將儲存庫與相依項目加入 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>
直接下載
您也可以直接從 GroupDocs.Search for Java releases 下載最新版本。
若要使用試用授權,請前往 GroupDocs’ temporary license page 並依照說明取得免費試用。
如何使用密碼字典索引文件
您可以 建立可搜尋的索引,只要提供一個將檔案路徑對應到密碼的字典。引擎會在索引過程中自動取得正確的密碼。
概覽
將文件密碼儲存在簡單的鍵值映射中,讓索引引擎在需要時解鎖檔案。
步驟 1:定義索引與文件資料夾
String indexFolder = "YOUR_OUTPUT_DIRECTORY/IndexUsingPasswordDictionary";
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY"; // Path to password‑protected documents
步驟 2:建立索引
// Initialize the Index object in the specified directory
Index index = new Index(indexFolder);
步驟 3:新增文件密碼
// Add passwords for specific files using their absolute paths
String path1 = new File(documentsFolder + "/English.docx").getAbsolutePath();
index.getDictionaries().getDocumentPasswords().add(path1, "123456");
String path2 = new File(documentsFolder + "/SampleDocument.docx").getAbsolutePath();
index.getDictionaries().getDocumentPasswords().add(path2, "123456");
步驟 4:索引文件
// Automatically retrieve passwords from the dictionary during indexing
index.add(documentsFolder);
步驟 5:在索引中搜尋
String query = "sample OR increasing";
SearchResult result = index.search(query);
// Handle search results (e.g., display or process them)
Tip: 在正式環境中,請從安全保管庫(例如 Azure Key Vault 或 AWS Secrets Manager)載入密碼,而非硬編碼。
疑難排解
- 確認每組密碼與檔案實際的保護密碼相符。
- 再次檢查檔案路徑;錯誤的路徑會拋出
FileNotFoundException。
如何使用事件監聽器在需要密碼時索引文件?
您可以僅在引擎觸發需要密碼的事件時提供密碼,將憑證從記憶體中移除,並支援動態密碼解析。
當索引引擎遇到加密檔案時,會觸發回呼,您可以從安全儲存庫查詢密碼或即時計算。此方法較將所有密碼放入字典的做法降低了攻擊面。
概覽
透過事件回呼即時提供密碼,保持憑證儲存隔離。
步驟 1:定義索引與文件資料夾
String indexFolder = "YOUR_OUTPUT_DIRECTORY/IndexUsingPasswordEvent";
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY"; // Path to password‑protected documents
步驟 2:建立索引
// Initialize the Index object in the specified directory
Index index = new Index(indexFolder);
步驟 3:訂閱 password‑required 事件
index.getEvents().PasswordRequired.add(new EventHandler<PasswordRequiredEventArgs>() {
@Override
public void invoke(Object sender, PasswordRequiredEventArgs args) {
// Provide password for DOCX files when needed
if (args.getDocumentFullPath().endsWith(".docx")) {
args.setPassword("123456");
}
}
});
步驟 4:索引文件
// The event handler will supply passwords as required during indexing
index.add(documentsFolder);
步驟 5:在索引中搜尋
String query = "sample OR increasing";
SearchResult result = index.search(query);
// Handle search results (e.g., display or process them)
疑難排解
- 確保事件處理器涵蓋所有需要索引的檔案副檔名。
- 先以少量樣本檔案測試,以確認密碼正確套用。
實務應用
- Enterprise document management – 自動索引機密合約、HR 檔案與財務報表,同時保持靜態加密。
- Legal archives – 快速檢索案件檔案而不暴露敏感內容。
- Healthcare records – 索引患者 PDF 與 Word 文件,符合 HIPAA 要求。
效能考量
- Memory allocation – 為大量批次分配足夠的堆記憶體(
-Xmx2g或更高)。 - Parallel indexing – 使用
index.addAsync(...)或執行多執行緒索引,可在 8 核心機器上達到 每小時 200 萬份文件 的處理量。index.addAsync(...)為將文件排入非同步索引的方式。 - Index maintenance – 定期呼叫
index.optimize()以壓縮索引並提升查詢速度。
常見問題與解決方案
- Wrong password – 文件會被跳過並記錄警告。請再次檢查密碼來源。
- Unsupported format – 安裝必要的格式外掛或先將檔案轉換為支援類型再進行索引。
- Large files – 增加堆記憶體大小,並考慮將大型檔案分批索引,以避免
OutOfMemoryError。
常見問答
Q: How do I handle different file formats?
A: GroupDocs.Search 支援 120+ 種格式,包括 PDF、DOCX、XLSX、PPTX 與常見影像類型。必要時請安裝格式外掛。
Q: What happens if a password is wrong?
A: 文件會被跳過並記錄警告;請檢查您的密碼字典或事件處理器。
Q: Can I index files stored in the cloud?
A: 可以,但必須先下載至本機暫存資料夾,因為引擎只能處理檔案系統路徑。
Q: How can I improve search relevance?
A: 透過 IndexOptions 調整評分、加入同義詞,並使用進階查詢語法如 field:term~ 進行模糊匹配。IndexOptions 為控制評分與索引行為的設定物件。
Q: What should I do if indexing fails for some files?
A: 查看日誌輸出;常見原因包括缺少密碼、檔案損毀或不支援的格式。
資源
- GroupDocs.Search Documentation
- API Reference
- Download GroupDocs.Search
- GitHub Repository
- Free Support Forum
- Temporary License Information
依照本指南,您現在已掌握 如何在 Java 中搜尋多種檔案類型 以及 如何索引受密碼保護的文件,提升應用程式的安全性與可發現性。
最後更新: 2026-10-28
測試環境: GroupDocs.Search 25.4 for Java
作者: GroupDocs