Search by Attribute Java with GroupDocs.Search Guide
您是否想透過 Java 動態修改與索引文件屬性,提升文件管理系統的效能?您來對地方了!本教學深入探討如何利用功能強大的 GroupDocs.Search for Java 套件來 search by attribute java、變更已索引文件的屬性,並在索引過程中加入屬性。無論您是要建置搜尋解決方案或優化文件工作流程,掌握這些技巧都是關鍵。
Quick Answers
- 什麼是 “search by attribute java”? 這是利用附加在每個文件上的自訂中繼資料(屬性)來篩選搜尋結果的能力。
- 索引後可以修改屬性嗎? 可以——使用
AttributeChangeBatch以批次方式更新文件屬性。 - 如何在索引時加入屬性? 訂閱
FileIndexing事件,並以程式方式設定屬性。 - 需要授權嗎? 免費試用可用於評估;正式環境須購買永久授權。
- 需要哪個 Java 版本? 建議使用 Java 8 或更新版本。
What is “search by attribute java”?
Search by attribute java 讓您可以根據文件的中繼資料(屬性)而非僅內容來查詢文件。只要為每個檔案附加 public、main、key 等鍵值對,即可快速縮小結果至最相關的子集合。
Why modify or add attributes?
- 動態分類 – 讓中繼資料與業務規則保持同步。
- 更快的篩選 – 屬性過濾在全文搜尋之前先行評估,提升效能。
- 合規追蹤 – 為文件加上保留政策或稽核需求的標籤。
Prerequisites
- Java 8+(JDK 8 或更新)
- GroupDocs.Search for Java 套件(請參考下方 Maven 設定)
- 具備基本的 Java 與索引概念
Setting Up GroupDocs.Search for Java
Maven Setup
<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>
Direct Download
Alternatively, download the latest version from GroupDocs.Search for Java releases.
If you prefer not using a build tool like Maven, download the JAR from the GroupDocs website.
License Acquisition
- Start with a free trial to explore capabilities.
- For extended use, obtain a temporary or full license via the license page.
Basic Initialization
import com.groupdocs.search.Index;
// Initialize an index in a specified directory
Index index = new Index("YOUR_OUTPUT_DIRECTORY/ChangeAttributes");
Implementation Guide
Search by Attribute Java – Changing Document Attributes
Overview
You can add, remove, or replace attributes on already indexed documents, enabling batch update document attributes without re‑indexing the whole collection.
Step‑by‑Step
Step 1: Add Documents to Index
index.add("YOUR_DOCUMENT_DIRECTORY");
Step 2: Retrieve Indexed Document Information
import com.groupdocs.search.results.DocumentInfo;
DocumentInfo[] documents = index.getIndexedDocuments();
Step 3: Batch Update Document Attributes
import com.groupdocs.search.common.AttributeChangeBatch;
import com.groupdocs.search.SearchOptions;
AttributeChangeBatch batch = new AttributeChangeBatch();
batch.addToAll("public"); // Add 'public' to all documents
batch.remove(documents[0].getFilePath(), "public"); // Remove 'public' from a specific document
batch.add(documents[0].getFilePath(), "main", "key"); // Add 'main' and 'key' attributes
// Apply changes
index.changeAttributes(batch);
Step 4: Search with Attribute Filters
import com.groupdocs.search.results.SearchResult;
SearchOptions options = new SearchOptions();
options.setSearchDocumentFilter(SearchDocumentFilter.createAttribute("main"));
String query = "length";
SearchResult result = index.search(query, options); // Perform the search
Batch Update Document Attributes with AttributeChangeBatch
The AttributeChangeBatch class is the core tool for batch update document attributes. By grouping changes into a single batch, you reduce I/O overhead and keep the index consistent.
Search by Attribute Java – Adding Attributes During Indexing
Overview
Hook into the FileIndexing event to assign custom attributes as each file is added to the index.
Step‑by‑Step
Step 1: Subscribe to the FileIndexing Event
import com.groupdocs.search.events.EventHandler;
import com.groupdocs.search.events.FileIndexingEventArgs;
index.getEvents().FileIndexing.add(new EventHandler<FileIndexingEventArgs>() {
@Override
public void invoke(Object sender, FileIndexingEventArgs args) {
if (args.getDocumentFullPath().endsWith("Lorem ipsum.pdf")) {
args.setAttributes(new String[] { "main", "key" });
}
}
});
Step 2: Index Documents
index.add("YOUR_DOCUMENT_DIRECTORY");
Practical Applications
- Document Management Systems – Automate categorization by adding metadata during ingestion.
- Large Content Archives – Use attribute filters to narrow searches, dramatically cutting response times.
- Compliance & Reporting – Dynamically tag documents for retention schedules or audit trails.
Performance Considerations
- Memory Management – Monitor JVM heap and tune
-Xmxas needed. - Batch Processing – Group attribute changes with
AttributeChangeBatchto minimize index writes. - Library Updates – Keep GroupDocs.Search up‑to‑date to benefit from performance patches.
Frequently Asked Questions
Q: What are the prerequisites for using GroupDocs.Search in Java?
A: You need Java 8+, the GroupDocs.Search library, and basic knowledge of indexing concepts.
Q: How do I install GroupDocs.Search via Maven?
A: Add the repository and dependency shown in the Maven Setup section to your pom.xml.
Q: Can I modify attributes after documents are indexed?
A: Yes, use AttributeChangeBatch to batch update document attributes without re‑indexing.
Q: What if my indexing process is slow?
A: Optimize JVM memory settings, use batch updates, and ensure you’re on the latest library version.
Q: Where can I find more resources on GroupDocs.Search for Java?
A: Visit the official documentation or explore community forums.
Resources
- Documentation: GroupDocs.Search for Java Docs
- API Reference: API Reference
- Download: Latest Releases
- GitHub: GitHub GroupDocs.Search
- Free Support Forum: GroupDocs Forums
- Temporary License: License Page
Last Updated: 2025-12-24
Tested With: GroupDocs.Search 25.4 for Java
Author: GroupDocs