Highlight search results Java – Synchronous & async indexing
In this guide you’ll discover how to highlight search results Java using the GroupDocs.Search library, and you’ll see step‑by‑step how to index documents Java both synchronously and asynchronously. Whether you are building a small desktop tool or a large‑scale enterprise search service, these techniques let you deliver instant, visually clear matches without blocking your application threads.
Quick answers
- What does “highlight search results Java” mean? It means wrapping each matched term in the returned snippets with markup (e.g.,
<mark>) so users can instantly see the context of the hit. - When should I use synchronous indexing? Use it for small‑to‑medium collections where you need the document searchable the moment it’s added.
- When is asynchronous indexing preferable? Choose it for large batches or when the UI thread must stay responsive while the index builds in the background.
- Do I need a license? A free trial works for development; a full license removes limits and unlocks advanced features.
- Which Java version is supported? Java 8 or later.
What is “highlight search results Java”?
highlight search results java is the process of taking raw match data from GroupDocs.Search and inserting visual cues—typically HTML <mark> tags—around each found term. This makes the result snippets instantly readable in a web page or Swing component, improving user experience by showing exactly where the query appears.
Why use GroupDocs.Search for Java?
GroupDocs.Search delivers a high‑performance, language‑agnostic engine that can process up to 5 000 documents per second, support 30+ file formats, and index 10 million‑document collections without loading the entire corpus into memory. Its built‑in highlighting, real‑time indexing, and multi‑language analyzers make it ideal for content‑management systems, e‑commerce catalogs, and enterprise document repositories.
Prerequisites
- Java Development Kit (JDK 8 or newer) installed and
JAVA_HOMEcorrectly set. - An IDE such as IntelliJ IDEA or Eclipse.
- A folder (e.g.,
documents/) containing the files you want to index—plain text, PDF, DOCX, etc. - Maven for dependency management (or you can manually add the JAR).
Required libraries and dependencies
Add GroupDocs.Search to your 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>
For direct downloads, get the latest version from GroupDocs.Search for Java releases.
Environment setup
- Verify
JAVA_HOMEpoints to a compatible JDK. - Create a new Maven project and paste the snippet above into the
<dependencies>section. - Place sample files in a directory like
src/main/resources/documents/.
How to set up GroupDocs.Search for Java
Index is the core class representing a searchable collection stored on disk.
Create an Index instance pointing to a folder on disk, apply a license if you have one, and optionally configure an analyzer for language‑specific tokenization. This preparation step ensures the engine can read, write, and search the index efficiently properly.
The Index class is the core component that represents a searchable collection on disk. After you instantiate it, all indexing and query operations flow through this object.
- Install the library – Use the Maven snippet above or download the JAR from GroupDocs.
- Obtain a license – Start with a trial license; replace it with a production key before deployment.
- 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");
How to highlight search results Java – synchronous indexing
DocumentHighlighter is a utility class that generates highlighted snippets from search results.
Load the index, add documents with index.add(documentPath), run a query, and then call DocumentHighlighter to wrap matches in <mark> tags. The whole process runs on the calling thread, so the document becomes searchable immediately after add returns for end users.
Step 1: create the index and attach error handling
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());
}
});
Step 2: add documents and run a search
// Add documents
index.add(documentsFolder);
// Perform a search
String query = "tincidunt";
SearchResult result = index.search(query);
Step 3: process results and 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);
}
}
}
How to highlight search results Java – asynchronous indexing
IndexingOptions configures how the indexing process runs, including synchronous or asynchronous mode.
Configure the IndexingOptions to run in background mode, subscribe to StatusChanged events, and let the engine index files while your UI continues to serve other requests. Once the status changes to Ready, you can execute searches and obtain highlighted snippets just like in synchronous mode.
The AsyncIndexingListener receives progress updates, allowing you to display a progress bar or log status without blocking the main thread.
Step 1: set up the index with 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.");
}
}
});
Step 2: enable asynchronous mode and start indexing
// Set up async indexing options
IndexingOptions options = new IndexingOptions();
options.setAsync(true);
// Add documents asynchronously
index.add(documentsFolder, options);
}
}
How to index documents Java – practical tips
index.update(path) updates an existing document in the index with the file at the specified path.
Break large collections into batches of 1 000–5 000 files, filter by extension to avoid unnecessary parsing, and use index.update(path) for changed files instead of rebuilding the whole index. These practices keep memory usage low and indexing time predictable to maintain consistency.
- Batch size: For huge collections, split the folder into smaller batches to avoid memory spikes.
- File filters: Use
IndexingOptions.setFileExtensionsto include only the formats you need (e.g.,.pdf,.docx). - Re‑indexing: When a document changes, call
index.update(documentPath)rather than recreating the index from scratch.
Performance considerations
- Memory: Monitor heap usage; increase
-Xmxif you process many large files simultaneously. - CPU: Asynchronous indexing spreads the workload across threads but still consumes CPU—track usage with JVisualVM.
- Result highlighting: Highlighting adds a modest overhead (≈ 2–5 ms per result). Cache the generated HTML if you need to display the same snippets repeatedly.
Frequently asked questions
Q: Can I combine synchronous and asynchronous indexing in the same application?
A: Yes. Use synchronous indexing for small, frequently updated sets and asynchronous indexing for bulk imports or background jobs.
Q: How do I customize the highlight style?
A: Provide a custom DocumentHighlighter implementation that writes the desired HTML, CSS, or XML tags around matched terms.
Q: What file types does GroupDocs.Search support out of the box?
A: Text, PDF, DOC/DOCX, XLS/XLSX, PPT/PPTX, HTML, and many more via built‑in parsers—over 30 formats in total.
Q: Is it possible to search in multiple languages simultaneously?
A: Absolutely. GroupDocs.Search includes multi‑language analyzers; just configure the appropriate Analyzer when creating the index.
Q: How do I secure the index folder?
A: Store the index in a protected directory, set strict file‑system permissions, and optionally encrypt the index using the library’s security features.
Last Updated: 2026-09-11
Tested With: GroupDocs.Search 25.4 for Java
Author: GroupDocs