Руководство по поиску по атрибуту Java с GroupDocs.Search

Ищете способ улучшить систему управления документами, динамически изменяя и индексируя атрибуты документов с помощью Java? Вы попали по адресу! В этом руководстве мы подробно рассмотрим, как использовать мощную библиотеку GroupDocs.Search for Java для search by attribute java, изменения проиндексированных атрибутов документов и их добавления во время процесса индексации. Независимо от того, создаёте ли вы поисковый портал, архив соответствия требованиям или интеллектуальное приложение, управляемое контентом, освоение этих техник сэкономит ваше время и повысит производительность.

Быстрые ответы

  • Что такое “search by attribute java”? Это возможность фильтровать результаты поиска, используя пользовательские метаданные, прикреплённые к каждому документу.
  • Можно ли изменять атрибуты после индексации? Да — используйте AttributeChangeBatch для пакетного обновления атрибутов документов.
  • Как добавить атрибуты во время индексации? Подпишитесь на событие FileIndexing и задавайте атрибуты программно.
  • Нужна ли лицензия? Бесплатная пробная версия подходит для оценки; для продакшна требуется постоянная лицензия.
  • Какая версия Java требуется? Рекомендуется Java 8 или новее.

Что такое “search by attribute java”?

Search by attribute java позволяет выполнять запросы к документам на основе их метаданных (атрибутов), а не только их содержимого. Привязывая пары «ключ‑значение», такие как public, main или key, к каждому файлу, вы можете быстро сузить результаты до наиболее релевантного подмножества.

Почему стоит использовать динамическое тегирование метаданных?

  • Dynamic categorization – поддерживайте метаданные в актуальном состоянии в соответствии с меняющимися бизнес‑правилами.
  • Faster filtering – фильтры по атрибутам оцениваются до полнотекстового поиска, ускоряя время отклика.
  • Compliance tracking – помечайте документы для политик удержания или требований аудита.
  • Batch update attributes – изменяйте множество документов одной операцией без полной переиндексации.

Предварительные требования

  • Java 8+ (JDK 8 или новее)
  • GroupDocs.Search for Java library (см. настройку Maven ниже)
  • Базовое понимание Java и концепций индексации

Настройка 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");

Как изменить атрибуты документов (пакетное обновление)

Search by Attribute Java – Changing Document Attributes

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

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");

Практические применения

  1. Document Management Systems – Automate categorization by adding metadata during ingestion.
  2. Large Content Archives – Use attribute filters to narrow searches, dramatically cutting response times.
  3. Compliance & Reporting – Dynamically tag documents for retention schedules or audit trails.

Соображения по производительности

  • Memory Management – Monitor JVM heap and tune -Xmx as needed.
  • Batch Processing – Group attribute changes with AttributeChangeBatch to minimize index writes.
  • Library Updates – Keep GroupDocs.Search up‑to‑date to benefit from performance patches.

Распространённые проблемы и решения

IssueWhy It HappensHow to Fix
Attributes not appliedEvent handler not registered before indexingEnsure index.getEvents().FileIndexing.add(...) runs before index.add(...).
Search returns no resultsAttribute name mismatch (case‑sensitive)Use exact attribute names when creating filters (createAttribute("main")).
Out‑of‑memory errors on large batchesToo many changes in a single batchSplit large updates into smaller AttributeChangeBatch instances.
License not recognizedUsing trial JAR without applying license fileCall License license = new License(); license.setLicense("path/to/license.file"); before any index operation.

Часто задаваемые вопросы

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.

Ресурсы


Last Updated: 2026-02-24
Tested With: GroupDocs.Search 25.4 for Java
Author: GroupDocs