Markeer zoekresultaten Java – Synchronous & Async Indexing

Boost your Java applications by highlighting search results Java with the powerful GroupDocs.Search library. Whether you’re dealing with a few files or a massive repository, mastering both synchronous and asynchronous indexing lets you deliver fast, accurate results without blocking your application threads.

Snelle antwoorden

  • Wat betekent “highlight search results Java”? Het verwijst naar het weergeven van overeenkomende termen in zoekresultaten met visuele aanwijzingen (bijv. HTML <mark>‑tags) zodat gebruikers kunnen zien waar de zoekopdracht in elk document voorkomt.
  • Wanneer moet ik synchronous indexing gebruiken? Voor kleine tot middelgrote datasets waarbij directe beschikbaarheid van nieuw toegevoegde documenten vereist is.
  • Wanneer is asynchronous indexing beter? Bij het verwerken van grote documentverzamelingen of wanneer je op een UI‑thread werkt en de applicatie responsief moet blijven.
  • Heb ik een licentie nodig? Een gratis proefversie werkt voor ontwikkeling; een volledige licentie ontgrendelt geavanceerde functies en verwijdert gebruikslimieten.
  • Welke Java‑versie wordt ondersteund? Java 8 of later.

Wat is “highlight search results Java”?

Highlighting search results in Java means taking the raw matches returned by GroupDocs.Search and wrapping the matching terms in HTML (or another markup) so they stand out when displayed in a UI or web page. This improves user experience by instantly showing the context of each hit.

Waarom GroupDocs.Search voor Java gebruiken?

GroupDocs.Search provides a high‑performance, language‑agnostic engine that supports:

  • Real‑time indexing and searching
  • Asynchronous processing for large workloads
  • Built‑in result highlighting
  • Multi‑language and custom analyzer support

These capabilities make it ideal for content management systems, e‑commerce catalogs, and enterprise document repositories.

Vereisten

Before you start, make sure you have:

  • Java Development Kit (JDK 8 or newer) installed.
  • An IDE such as IntelliJ IDEA or Eclipse.
  • A folder containing the documents you want to index.
  • Maven for dependency management (or you can download the JAR manually).

Vereiste bibliotheken en afhankelijkheden

Add GroupDocs.Search to your Maven project:

<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>

For direct downloads, get the latest version from GroupDocs.Search for Java releases.

Omgevingsconfiguratie

  • Verify your JAVA_HOME points to a compatible JDK.
  • Create a project in your IDE and add the Maven configuration above.
  • Prepare a directory (e.g., documents/) with sample text, PDF, or Word files.

Hoe GroupDocs.Search voor Java in te stellen

  1. Install the Library – Use the Maven snippet above or download the JAR from GroupDocs.
  2. Obtain a License – Start with a trial license, then upgrade when you move to production.
  3. Initialize the Index – The following snippet shows how to create (or open) an index folder:
import com.groupdocs.search.Index;

// Create an index in the specified folder
Index index = new Index("path/to/index/folder");

Hoe highlight search results Java – Synchronous Indexing

Synchronous indexing processes documents immediately, making newly added files searchable right away.

Stap 1: Maak de index aan en voeg foutafhandeling toe

import com.groupdocs.search.*;
import com.groupdocs.search.events.*;
import java.nio.file.Paths;

public class SynchronousIndexingFeature {
    public static void main(String[] args) {
        String indexFolder = "YOUR_DOCUMENT_DIRECTORY/SynchronousIndexing";
        String documentsFolder = YOUR_DOCUMENT_DIRECTORY; // Replace with actual directory path

        Index index = new Index(indexFolder);

        // Handle errors
        index.getEvents().ErrorOccurred.add(new EventHandler<IndexErrorEventArgs>() {
            @Override
            public void invoke(Object sender, IndexErrorEventArgs args) {
                System.out.println(args.getMessage());
            }
        });

Stap 2: Voeg documenten toe en voer een zoekopdracht uit

        // Add documents
        index.add(documentsFolder);

        // Perform a search
        String query = "tincidunt";
        SearchResult result = index.search(query);

Stap 3: Verwerk resultaten en highlight search results Java

        for (int i = 0; i < result.getDocumentCount(); i++) {
            FoundDocument document = result.getFoundDocument(i);
            System.out.println(": Document: " + document.getDocumentInfo().getFilePath());
            System.out.println(": Occurrences: " + document.getOccurrenceCount());
        }

        // Highlight results
        if (result.getDocumentCount() > 0) {
            FoundDocument document = result.getFoundDocument(0);
            String path = YOUR_OUTPUT_DIRECTORY + "/Highlighted.html";
            OutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, path);
            DocumentHighlighter highlighter = new DocumentHighlighter(outputAdapter);
            index.highlight(document, highlighter);
        }
    }
}

The DocumentHighlighter automatically wraps matched terms with <mark> tags (or any format you configure), giving you highlighted search results ready for display.

Hoe highlight search results Java – Asynchronous Indexing

When dealing with thousands of files, blocking the main thread is undesirable. Asynchronous indexing lets the engine work in the background.

Stap 1: Stel de index in met event listeners

import com.groupdocs.search.*;
import com.groupdocs.search.events.*;

public class AsynchronousIndexingFeature {
    public static void main(String[] args) {
        String indexFolder = "YOUR_DOCUMENT_DIRECTORY/AsynchronousIndexing";
        String documentsFolder = YOUR_DOCUMENT_DIRECTORY; // Replace with actual directory path

        Index index = new Index(indexFolder);

        // Handle errors and status changes
        index.getEvents().ErrorOccurred.add(new EventHandler<IndexErrorEventArgs>() {
            @Override
            public void invoke(Object sender, IndexErrorEventArgs args) {
                System.out.println(args.getMessage());
            }
        });

        index.getEvents().StatusChanged.add(new EventHandler<BaseIndexEventArgs>() {
            @Override
            public void invoke(Object sender, BaseIndexEventArgs args) {
                if (args.getStatus() != IndexStatus.Ready || args.getStatus() == IndexStatus.Failed) {
                    System.out.println("Indexing completed.");
                }
            }
        });

Stap 2: Schakel asynchrone modus in en start indexering

        // Set up async indexing options
        IndexingOptions options = new IndexingOptions();
        options.setAsync(true);

        // Add documents asynchronously
        index.add(documentsFolder, options);
    }
}

While the index is being built, your application can continue to serve other requests. Once the StatusChanged event reports Ready, you can safely run searches and obtain highlighted search results Java.

Hoe index documents java – Praktische tips

  • Batch size: For huge collections, split the folder into smaller batches to avoid memory spikes.
  • File filters: Use IndexingOptions.setFileExtensions to include only the formats you need (e.g., .pdf, .docx).
  • Re‑indexing: When documents change, call index.update(documentPath) instead of rebuilding the whole index.

Prestatieoverwegingen

  • Memory: Keep an eye on heap usage; increase -Xmx if you process many large files.
  • CPU: Asynchronous indexing spreads the workload, but still consumes CPU—monitor with JVisualVM.
  • Result Highlighting: Highlighting adds a small processing overhead; cache the generated HTML if you need to display results repeatedly.

Veelgestelde vragen

Q: Kan ik synchronous en asynchronous indexing combineren in dezelfde applicatie?
A: Ja. Gebruik synchronous indexing voor kleine, vaak bijgewerkte sets en asynchronous indexing voor bulk‑imports of achtergrondtaken.

Q: Hoe pas ik de highlight‑stijl aan?
A: Provide a custom DocumentHighlighter implementation that writes the desired HTML, CSS, or XML tags around matched terms.

Q: Welke bestandstypen ondersteunt GroupDocs.Search out of the box?
A: Text, PDF, DOC/DOCX, XLS/XLSX, PPT/PPTX, HTML, and many more via its built‑in parsers.

Q: Is het mogelijk om tegelijk in meerdere talen te zoeken?
A: Absolutely. GroupDocs.Search includes multi‑language analyzers; just configure the appropriate Analyzer when creating the index.

Q: Hoe beveilig ik de indexmap?
A: Store the index in a protected directory, set proper file system permissions, and consider encrypting the index with the library’s security features.


Last Updated: 2026-02-08
Tested With: GroupDocs.Search 25.4 for Java
Auteur: GroupDocs