搜尋中的停用詞:使用 GroupDocs.Search Java 將文件加入索引
如果您需要 add documents to index 同時確保沒有重要的詞彙——尤其是常見詞——被忽略,您來對地方了。在本指南中,我們將示範如何使用 GroupDocs.Search for Java disable stop words in search,讓每個標記(即使是 “on”、 “by” 或 “the”)都可被搜尋,從而使結果更為精確。
快速解答
- What does “add documents to index” mean? 這表示將您的來源檔案載入可搜尋的索引,以便能有效地查詢。
- Why would I disable stop words? 為了在搜尋中納入常見詞彙(例如 “on”、 “the”),當這些詞在您的領域中具有意義時。
- Which library version is required? 需要 GroupDocs.Search for Java 25.4 或更新版本。
- Do I need a license? 免費試用可用於評估;正式環境需購買永久授權。
- Can I use this in a Maven project? 可以——只需在下方加入相應的倉庫與相依性。
什麼是搜尋中的停用詞,為什麼可能想要停用它們?
停用詞是許多搜尋引擎自動過濾的高頻詞彙,以加快查詢速度。雖然這能提升一般網路搜尋的效能,但在專業領域——如法律合約、電子商務目錄或技術手冊——中,像 “on”、 “by” 或 “as” 這類詞彙具有實際意義,過濾它們會降低精確度。停用停用詞可讓每個詞都被視為重要,確保不會錯過任何相關文件。
在 GroupDocs.Search 中,加入文件至索引的運作方式是什麼?
當您加入文件時,函式庫會讀取每個檔案,將其內容斷詞,並將斷詞結果儲存在最佳化的資料結構(即索引)中。完成索引後,搜尋引擎能在毫秒內取得相符的文件,即使面對大型集合亦是如此。
前置條件
- Required Libraries: GroupDocs.Search for Java 25.4(或更新版本)。
- Development Environment: IntelliJ IDEA、Eclipse,或您偏好的任何 Java IDE。
- Basic Knowledge: 熟悉 Java 語法與索引概念。
設定 GroupDocs.Search for Java
Maven 安裝
如果您使用 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 下載最新版本。
取得授權步驟
- Free Trial – 立即開始測試。
- Temporary License – 取得限時金鑰以獲得完整功能。
- Purchase – 購買永久授權以供正式使用。
基本初始化與設定
建立 IndexSettings 的實例,以控制索引的行為:
import com.groupdocs.search.IndexSettings;
// Create an instance of IndexSettings
IndexSettings settings = new IndexSettings();
如何在搜尋中停用停用詞(Java)
以下程式碼可關閉內建的停用詞過濾器:
// Disable the use of stop words
tsettings.setUseStopWords(false);
Parameters: setUseStopWords 接受布林值。
Purpose: 確保每個詞彙——包括常見的停用詞——皆被索引且可搜尋。
如何將文件加入索引
定義輸出目錄
import com.groupdocs.search.Index;
// Define the path to the output directory for indexing
String indexFolder = "YOUR_OUTPUT_DIRECTORY\\IndexingWithStopWords";
// Create an index at the specified location with the configured settings
Index index = new Index(indexFolder, settings);
指定文件目錄
// Define the path to your document directory
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY";
// Add all documents in the specified folder to the index
index.add(documentsFolder);
現在 YOUR_DOCUMENT_DIRECTORY 中的每個檔案都已 added documents to index,並可供查詢。
執行搜尋查詢
import com.groupdocs.search.results.SearchResult;
// Define your search query
tString query = "on";
// Perform the search operation using the index and the specified query
SearchResult result = index.search(query);
由於已停用停用詞,搜尋時會考慮詞彙 "on",返回原本會被忽略的匹配結果。
實務應用
- Enterprise Document Search – 確保關鍵術語不會被過濾。
- E‑commerce Platforms – 透過索引商品描述中的每個詞彙,提升產品搜尋的發現率。
- Legal Research Tools – 捕捉每個法律用語,即使是常被視為停用詞的詞彙。
效能考量
- Optimization Tips: 定期更新與修剪索引,以維持高搜尋速度。
- Resource Usage: 監控 JVM 堆積大小;大型索引可能需要調整垃圾回收設定。
- Java Memory Management: 使用高效的資料結構,對於極大規模的語料庫可考慮使用堆外儲存。
常見問題與解決方案
| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| 常見詞彙無結果 | setUseStopWords(true)(預設) | 如上所示,呼叫 setUseStopWords(false)。 |
| 索引期間記憶體不足錯誤 | 同時索引過多大型檔案 | 分批索引檔案;增加 -Xmx JVM 參數。 |
| 搜尋返回過時資料 | 新增檔案後未刷新索引 | 呼叫 index.update() 或重新加入變更的文件。 |
常見問答
Q: What are stop words?
A: 停用詞是許多搜尋引擎為加快查詢而忽略的常見詞彙(例如 “the”、 “is”、 “on”)。停用它們可讓每個標記皆可被搜尋。
Q: Why disable stop words in search indexes?
A: 當需要精確片語匹配——例如在法律或技術文件中——每個詞都有意義,必須納入停用詞。
Q: How does GroupDocs.Search handle large datasets?
A: 此函式庫使用最佳化的資料結構與增量索引,讓記憶體使用保持低位,即使面對數百萬文件亦是如此。
Q: Can I integrate GroupDocs.Search with other Java applications?
A: 可以,API 設計易於嵌入任何基於 Java 的系統,從 Web 服務到桌面應用程式皆可。
Q: What should I do if my search results are not accurate?
A: 確認索引已包含所有必要文件(add documents to index),如有需要確保已停用停用詞過濾,並在重大變更後考慮重新建構索引。
其他資源
- 文件說明: GroupDocs Search Documentation
- API 參考: GroupDocs API Reference
- 下載: Get the latest GroupDocs.Search for Java
- GitHub 倉庫: Explore on GitHub
- 免費支援: Join GroupDocs Forum
- 臨時授權: Apply for a Temporary License
透過本指南,您現在已了解如何 add documents to index 與 disable stop words in search,以在 Java 應用程式中提供更精確的搜尋結果。
最後更新: 2026-02-19
測試環境: GroupDocs.Search for Java 25.4
作者: GroupDocs